Help - Make a tile loop after a random time in one room

Hi everyone,
I'm trying to make a loop on a shooting star animation in one room, using a delay with "wait" function.
As I'm not a programmer I tried many thing and eventually got something almost working :slight_smile:

on enter do
	starLoop = 1
	starTime = random 1,5
	call "starAct"
	
end

on exit do
	starLoop = 0
	end

on starAct do
	
	if starLoop==1 then
				wait starTime then
			tell 22,3 to
				swap "starAnim1"
				wait 0.3 then
					swap "black"
				end
			end
			tell 21,3 to
				swap "starAnim2"
				wait 0.3 then
					swap "black"
				end
			end
			tell 21,4 to
				swap "starAnim3"
				wait 0.3 then
					swap "black"
				end
end
		wait 5 then
call "starAct"
end
end
end
end

On next room I use this code too, to make sure the star will not appear again :

on enter do
	starLoop = 0
end

But it happen that the star still apprear and make a mess in my tiles in 2nd room...
I think that's happen when the player enter next room as the function "starAct" is still on process, but I have no idea how to stop it :dotted_line_face:

I’d first solve the problem of recursion. Recursion means calling a function in the function itself. On modern computers this can make sense, but on Playdate with its limited processing power it should often be avoided. I’d suggest you use the loop event instead:

on loop do
  if starLoop == 1 then
    timer++
    if timer == starTime then
      tell event.room to  //edit: changed to event.room
        call "starAct"
        timer = 0
      end
    end
  end
end

With this you can avoid recursion. starAct will be called when starLoop==1 and a certain amount of frames have elapsed, counted by timer. starTime can be determined somewhere else, like in the enter event, like you did. If you use the star in multiple room I’d suggest using emit „starAct“ instead of call „StarAct“.

To solve the problem you were asking about, I’d add another check if starLoop==1 before switching back to black tiles. With the changes above and a bit of restructuring to avoid duplicate code, that would result in the following:

on starAct do
	
    tell 22,3 to
		swap "starAnim1"
    end
    tell 21,3 to
		swap "starAnim2"
    end
	tell 21,4 to
		swap "starAnim3"
    end

    wait 0.3 then
        if starLoop==1 then
            tell 22,3 to
                swap "black"
            end
            tell 21,3 to
                swap "black"
            end
            tell 21,4 to
                swap "black"
            end
        end
    end
       	
end

So now, before anything is changed, there’s a check, whether starLoop==1. This should solve your problem. Hope this helps :slight_smile:

(P.S.: You could also just ignore everything above and add an if starLoop==1 before every swap. That should also do the trick.)

2 Likes

Thanks for your help Kucromy ! :slight_smile:

I tried the loop function but I can't make it works... T_T
I passed the hole morning to figure out how to do but I'm stuck...

would you mind take a look ? I erased all other part of the game to focus on my problem. Just want that shooting star to loop after a random time @_@
Etoiles Filantes.json.zip (6.6 KB)

I think @Kucromy offered some great advice - and as they said, fixing recursion is a big deal here.

I suggest using the tiles themselves to automatically handle things... something like this (a really rough first pass at scripting this up):
Test.zip (5.0 KB)

test

my star animations are REALLY simple, so the animation might look clunky... but the approach ought to be sound.

1 Like

Thanks Bitflung, I going to try out you script right away !

mmm your .json file seems to be corrupted, I can't upload it :confused:

The JSON is corrupted? Strange. You did unzip it right?

I'm on the road now but can test and resend when I get home.

1 Like

Yes it's strange, it comes on my Games list but when I open it, it restore the last deleted game instead ^^'

Okay, I caught the mistake:

• The loop event has to be called in the game script, not in a room script.
tell can’t target a room other than the one you’re in (That was my mistake, sorry). In the loop event please correct the code like so (I also corrected it in my previous answer):

tell event.room to
	call "starAct"
	timer = 0
end

After that the animation is working, but doesn’t really look smooth, as it starts at a random point and the length is also too short. The latter can be fixed by waiting not 0.3 but 0.5 seconds before switching back to black (Pulp runs at 20fps and your star animation is 10fps). Also startime = random 2,5 should rather be startime = random 20,50.

I can’t seem to find a fix for the first issue (starting point of animation) right now, maybe someone else can help there. The animation should essentially start at frame 0, but afaik tile animations always run in the background, so you can’t tell it to start at 0. You can only set frames of non-animated tiles. I also played around with checking, whether the game is in a 10th frame to swap in the tiles at the right time, but couldn’t get it to work.

(Btw, your game art look gorgeous!!)

1 Like

Great ! thanks a lot for the time you giving me :pray:
I'm going to try this and fix it, hope I can go forward :slight_smile:

(Btw, your game art look gorgeous!!)

Thank, can't wait to show you the full game :blush:

i'm back now. i can't reproduce your issue with the import - it works fine here... i just renamed my test game to "bitflung-test" and exported again.
bitflung-test.zip (5.0 KB)

Hopefully that works. sounds like you've already resolved your issues, but all the same i'd like to know that json imports are working?

Hey thanks for the export,

That's weird, when I import a game it's comes on my game list, but when I try to open it, it turns back to the old game... But I found a way, after I import your game I click on room button instead and here it is !
So now I can see your code :partying_face:

Thank you very much ! You way looks great too I will totally try to use it too :smiley:

I found a way for the starting point of animation's issue, I simply add play instead swap !
Then it will only play the animation once after it swap again into black tile ^^

on starAct do
	
	tell 22,3 to
		//swap "starAnim1"
		play "starAnim1" then
			swap "black"
		end
	end
	tell 21,3 to
		//swap "starAnim2"
		play "starAnim2" then
			swap "black"
		end
	end
	tell 21,4 to
		//swap "starAnim3"
		play "starAnim3" then
			swap "black"
		end
	end
	
end
1 Like

You can just use play there - play will swap and run a single animation cycle.

1 Like

ho right thanks ! I correct my message :slight_smile: