Global Vars & Sprite Clones

I'm really struggling with the global variables...or I assume that's my problem.

I'm trying to implement a scrolling background by using animated sprites (14 frames at 10fps) that move x values using 'swap' at certain frame values. Once the animation completes (frame = 14), the tile should remove itself. Frustratingly, when I capture the frame of the tile instance using frame event.x,event.y, the variable is global and all instances of the sprite appear to execute based on a single instances value.

Additionally, tiles instantiated using swap appear to not always start on frame 0. My logging shows a mixed bag that leads me to believe there is a global inaccessible clock that manages sprite frames. I've tried getting around it by forcing frame 0 immediately after swap, but it seems ineffective.

The code below is called every other frame by the main game script's loop event (every other frame because game is at 20fps and tile animation is at 10fps). Any insights would be much appreciated.

on moveUpFingers do
	// capture current instance frame
	fUpFrame = frame event.x,event.y
	
	log "Current up finger frame = {fUpFrame}"
	
	if fUpFrame==14 then
		// if on final frame, remove animated tile by swapping with blank
		log "removing up finger at {event.x},{event.y}"
		
		tell event.x,event.y to
			swap "white"
		end
		
	elseif event.x!=0 then
		
		if fUpFrame==7 then
			// if at right frame, draw new tile to the left to show continuous scrolling animation
			newUpX = event.x
			newUpX -= 1
			
			tell newUpX,event.y to
				log "instantiate new up finger at {newUpX},{event.y}"
				swap "FingerUp"
				frame 0
			end
		end
	end
end

Tiles with a non-zero fps are indeed on a global timer and when swapped in, start on whichever frame the global timer for that tile is currently on.

Because you’re setting fUpFrame and newUpX each time this is called, all variables being global shouldn’t be an issue here.

For what you’re trying to achieve I’d suggest setting the tile’s fps to 0 and changing its frame manually. Or I would but there’s currently a bug that prevents getting the current frame of a 0 fps tile. I should have a fix for that up momentarily.

Thank you @shaun . Appreciate the clarification. I'll pursue the manual frame adjustment route by implementing an iterator variable for now.

Just following up - that worked way better than I thought. Thank you again.

Now on to implementing additional enemies, sprite collisions, and high scores...
FartyBird_playdate_FingerMovement

1 Like