FBA The Creator
http://www.fbacreator.com/fbaforum/

FBA GameGrid
http://www.fbacreator.com/fbaforum/viewtopic.php?f=5&t=137
Page 1 of 1

Author:  johngood [ Tue Jan 06, 2009 2:16 pm ]
Post subject:  FBA GameGrid

Hi,
I am new to FBA and I am trying to create a game using the GameGrid and it is going well, however when the player
does the wrong action I want him/she to explode, I can do this with DrawImage on the screen but how do I do it in
a Grid cell?
I have tried the following but I only see the last image no matter how long I make the delay! :(

function die()
local i
playsound(dead)

for i=12, 14 do --
gridcell(grid,manposx,manposy,i)
delay(10)
end
end

function delay(d)
for k = 1, d do
for j = 1, 10000 do
end
end
end

Author:  evilmaio [ Wed Jan 07, 2009 8:31 am ]
Post subject:  Re: FBA GameGrid

johngood wrote:
Hi,
I am new to FBA and I am trying to create a game using the GameGrid and it is going well, however when the player
does the wrong action I want him/she to explode, I can do this with DrawImage on the screen but how do I do it in
a Grid cell?
I have tried the following but I only see the last image no matter how long I make the delay! :(

function die()
local i
playsound(dead)

for i=12, 14 do --
gridcell(grid,manposx,manposy,i)
delay(10)
end
end

function delay(d)
for k = 1, d do
for j = 1, 10000 do
end
end
end


Hi and welcome,

putting a pause in your mainloop is a bad idea and you should avoid to insert ANY delay in your code ;)
In order to achieve what you want, you have to delay frames in other ways.
For example, providing you're game is running 30 FPS and you change the animation frame each 5 frames; you will play a 6 frames animation in about 1 second.

Your code could be transformed in this way:

Quote:
function onmainloop()
die()
end


local current_frame = 12 -- start from frame 12
local max_frames = 15 -- avoid to switch frames, when we reached the last one
local current_delay = 0
local frames_delay = 5 -- switch frame every 5 frames

function die()

-- be sure to play sound just once
if current_delay == 0 then
playsound(dead)
end

-- if we didn't reach the last frame, continue with our 'animation' system
if current_frame < max_frames then

current_delay = current_delay + 1

-- time to switch frame
if current_delay == frames_delay then
current_delay = 0
current_frame = current_frame + 1
end

end

-- draw animation
gridcell(grid,manposx,manposy,current_frame)

end


Regards

Author:  johngood [ Wed Jan 07, 2009 10:54 am ]
Post subject:  Re: FBA GameGrid

Hi evilmaio,

Thank you for your code snippet, I will try it out shortly. :D

The reason I created a delay function was mainly for the numkeypress because as I move my character around the grid he moves too quickly,
(at the slightest key touch the character will move 4 cells and if one of these cells is a no go area he will die, idealy I would like to move one cell at a time)
I have a delay at the end of each input:
If (numkeypress(******())>0)
-- some code
keydelay(15)
end

In VB6 my key input routine would use vbKeyUp with a DoEvents in a loop, the vbKeyUp is not the same as your's it is when the key is released, the DoEvents
allows other program code to continue.

How would you handle moving the character one cell at a time?
Here is my move right code sorry tabs dont seem to work in this area:

if (numkeypress(keyright()) > 0) then --------- KeyRight **************
if manposx == 15 then return true end

tile = gridcell(grid,manposx + 1, manposy)
if tile == 2 then return true end --wall detected
manwalking = true

------------------------- move a boulder ---------------
gridcell(grid,manposx, manposy, 9) --Walk right
if tile == 3 then --boulder
gridcell(grid,manposx, manposy, 7) --push right
test = gridcell(grid,manposx + 2, manposy)
if test ~= 0 then return true end
gridcell(grid,manposx + 2, manposy, 3)--boulder
gridcell(grid,manposx + 1, manposy, 7)--push reght
end

------------------------ collect a gem -----------------
if tile == 4 then
playsound(ding)
end
--------------------------------------------------------

gridcell(grid,manposx + 1,manposy,9)
gridcell(grid,manposx,manposy,0)
manposx = manposx + 1
debug("manposx = " ..manposx)
keydelay(15)
manwalking = false

end----------------- end if keyright ----------------------------------

Author:  evilmaio [ Wed Jan 07, 2009 1:10 pm ]
Post subject:  Re: FBA GameGrid

Ok, the game logic is different here, you have only a mainloop and there isn't nothing like "DoEvents"; your game logic should be slightly changed ;)

You could delay keypresses with something like that:

Quote:
if (numkeypress(keyright()) % 10 == 0)


altought this is one of the way you could achieve that, just use your fantasy ;)

Just remember: the onmainloop event must be as fast as possible and you must not pause or freeze your execution through "for loops" or some other code blocking tricks...
(This will freeze your game which won't run well)

Regards.

Author:  johngood [ Thu Jan 08, 2009 4:41 pm ]
Post subject:  Re: FBA GameGrid

Hi Again,

I tried your 'if (numkeypress(keyright()) % 10 == 0) then'

On starting the game the character runs straignt to the edge of the screen without a keypress! :(

also

I dont see how a delay on exiting the 'if (numkeypress(keyright()) > 0)' can cause any problems in the program loop as it
is only activated if the right arrow key is pressed, I also don't see '% 10 == 0' option in the FBA Help File!

Will this book help me in any way as the First Edition Online has no reference to 'Graphics'

Programming in Lua (second edition)

Author:  evilmaio [ Thu Jan 08, 2009 4:57 pm ]
Post subject:  Re: FBA GameGrid

Yeah, right,
because 0 % 10 = 0, just modify the condition as following:

Quote:
if ( numkeypress(keyright()) % 10 == 0 and numkeypress(keyright()) > 0 ) then



Quote:
I dont see how a delay on exiting the 'if (numkeypress(keyright()) > 0)' can cause any problems in the program loop as it
is only activated if the right arrow key is pressed


Try by yourself :)
Delaying the onmainloop function, means delay in the screen refresh (screen is refreshed after each onmainloop call)
For example, if anything else needs to be updated in the meanwhile (an explosion or something else), will be freezed while you're waiting for delay end!

Quote:
I also don't see '% 10 == 0' option in the FBA Help File!


% is the LUA (and some other languages) modulo operator and returns the rest of a division; (in this case: it means more or less: 'take a key every 10 steps').
It's referenced in FBA help: Language->Expressions->Arithmetic Operators.

Quote:
Will this book help me in any way as the First Edition Online has no reference to 'Graphics'
Programming in Lua (second edition)


You won't find any 'Graphics' reference in LUA manuals, LUA it's just a scripting language. Graphics functions are introduced by FBA and just for FBA use; they're fully covered by FBA help ;)

Regards.

Author:  johngood [ Sun Jan 11, 2009 10:52 am ]
Post subject:  Re: FBA GameGrid

Thank's this was a long discussion but very rewarding indeed for me, you have cleared up quite a few questions along the way. :D

The following statement now allows me to tweek the Key responce as the code increses in size and complexity, at the moment
a value of two is just right, I will probably change it to a variable later like 'KeyResp' as global.

if ( numkeypress(keyright()) % 2 == 0 and numkeypress(keyright()) > 0 ) then

Any chance in the future of Graphic Sprites and/or Animated Gifs? :)

Regards,
Andrew.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Limited
https://www.phpbb.com/