Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers AppGameKit Corner / some doubts about iteration and functions

Author
Message
edler
6
Years of Service
User Offline
Joined: 11th Jan 2018
Location:
Posted: 28th Mar 2018 08:57
FIRST DOUBT: Having a code...

playsprite(1,20)
DO
sync()
LOOP


animation works, but

DO
Playsprite(1,20)
sync()
LOOP


don't works. why?

SECOND DOUBT: with the code...

DO
asteroid()
sync()
LOOP

FUNCTION asteroid()
playsprite(1,20)
ENDFUNCTION

The animation don't works. But putting a condition inside function..

DO
asteroid()
sync()
LOOP

FUNCTION asteroid()
if getrawkeypressed(32) //spacebar
playsprite(1,20)
endif
ENDFUNCTION


works pressing the key. What have i do to plays without condition?

I thought that understaded the functions and iteration, but this dismantle all mi mind!!


thanks!!



--
edler

AppGameKit forum in Spanish, yet!
Dybing
13
Years of Service
User Offline
Joined: 12th Sep 2011
Location: Bergen, Norway
Posted: 28th Mar 2018 09:32 Edited at: 28th Mar 2018 09:37
Playsprite will start a sprite animation from frame 1 and at each sync() advance to next frame - so if you put that inside a (tight) loop, all you'll achieve is playing frame 1 again and again.

Same with your second example - it'll only run Playsprite() once when you press the key. At all other times, it'll just happily advance the sprite one frame at each sync() after being activated at least once.

The preferred way of organizing views is:

1: Initialization
Declare all local variables and structures of the function that will be needed (apart from what is imported in the function signature). Set initial values where needed.

2: Set-Up
Draw up all elements of the view.

3: The loop
3-1: Read input
3-2: Process data
3-3: Update elements in view
3-4: Sync

4: Tear-Down
Clear all sprites and text of view that will not carry over to next.

5: Exit function, with return values if need be.
edler
6
Years of Service
User Offline
Joined: 11th Jan 2018
Location:
Posted: 28th Mar 2018 11:36 Edited at: 28th Mar 2018 11:36
thanks!! but i dont understand still, sorry. because if the function has the code:

function anim()
if getspriteplaying(1)=0
playsprite(1,20)
endif
endfunction


But with this code don't works:

function anim()
if 1=1
playsprite(1,20)
endif
endfunction

Why?
--
edler

AppGameKit forum in Spanish, yet!
Dybing
13
Years of Service
User Offline
Joined: 12th Sep 2011
Location: Bergen, Norway
Posted: 28th Mar 2018 12:18
The first will only be true if your sprite is not playing, hence playsprite() will only be called once.

The second will always be true, so playsprite() will be called each and every time. So if you call upon your anim() function before each sync(), the sprite will always only display its' frame 1 as it get restarted each sync() cycle.

Playsprite() should only be called once to start a sprite-animation. Once called, each sync() will advance the sprite one frame in its' animation. The sprite will continue playing until either stopSprite() is called or deleteSprite() is called.
edler
6
Years of Service
User Offline
Joined: 11th Jan 2018
Location:
Posted: 28th Mar 2018 12:31
Yes! Thanks! Now I understand it completely!!

global a=0

do
anim()
sync()
loop

function anim()
if a=0
playsprite(1,11)
a=1
endif
endfunction


Only one call. EUREKA!!
--
edler

AppGameKit forum in Spanish, yet!
PHeMoX
6
Years of Service
User Offline
Joined: 9th Jan 2018
Location:
Posted: 28th Mar 2018 18:56
It's probably also a smart idea to always check whether something is already animating, before overwriting the start of animations.
Especially true for 3D animations. In fact, that stuff only works reliably when using 'Select' and 'Case' in a separate function I've noticed. Then just feed it the Case it should follow from elsewhere.



You could add extra conditions that will select a different cases in the main Do Loop. Just add the case code and point to it using animate(3,ObjectID) to tell it to go to case 3.

Login to post a reply

Server time is: 2024-11-21 19:58:31
Your offset time is: 2024-11-21 19:58:31