Dat crank tho! A place to share crank know-how

A Crank Based Crane Game

Someone asked me if it would be possible to control the player using the crank. After a bit of tinkering, it sure is!
chrome_2022-04-09_11-38-02
The process for moving the player is actually relatively simple. The ropes to the left and right of the player are just tiles that aren't collideable, so the player can move through them freely using left and right. However, the white space above and below the player are set to be collideable, so the player can't move through them using the arrows.

To move the player using the crank we just check every update to see if the crank has turned. If it has, we move the player up or down, based on the change in rotation:

on loop do
	// if the crank has turned, move the player
	if event.ra>0 then
		player_y += 1
	elseif event.ra<0 then
		player_y -= 1
	end
	
	// if the player is out of bounds, lock them back in  bounds
	if player_y<=0 then
		player_y = 1
	elseif player_y>=15 then
		player_y = 14
	end
	
	// if the y has changed, move the player to the new position
	if prev_py!=player_y then
		goto event.px,player_y
	end
end

Notice, we only need to handle changes in the y axis, because that's the only axis the crank moves in. We also add a short script to the player to update the previous position whenever they move.

on update do
	// update the previous position
	prev_px = event.px
	prev_py = player_y
end

Adding the Dangly Rope

It's a bit more work to add the rope left behind by the player sprite as they move up and down. The key here is to keep track of where the player moves from, and where they move to, and notify those tiles accordingly. Then, they can just swap as the player moves over them! I did this by adding the following code to the players update event, before the update to the previous position.

	// if we moved down, tell the old tile we left it
    // this is only done when moving downwards,
    // so we don't leave ropes while moving up.
	if prev_py<player_y then
		tell prev_px,prev_py to
			call "playerMovedFrom"
		end
	end
	
	// tell the new tile we entered it
	tell event.px,player_y to
		call "playerMovedTo"
	end

Then, all you have to do is add a handler to the tiles that you plan to move across to process these events! In my case, I have the empty tile swap to a rope when its left, and the rope swap to an empty tile when entered. I also have the top rope swap to a solid tile when exited so that the player can't move up and into it.
You can import this project from here.
As always have a great time with your Playdate!

1 Like