Moving a sprite

Hi !
I'd like to move a "cross" sprite depending on user crank input.

On the player tile:

on crank do
	crossY++
	emit "moveCross"
end

On the cross sprite tile:

on moveCross do
	goto crossX,crossY
end

But it doesn't work: the Player moves to crossX, crossY, NOT the cross sprite...
Any idea of what's wrong ?

1 Like

Unfortunatly goto only works on the player.

You could "draw" the cross sprite at that location instead.

draw "CrossTile" at crossX,crossY but you'd then need to log the last location (or the current location when you draw it) and then draw a blank / replacement tile under it. Or log what tile it replaces.

1 Like

goto only operates on the player tile. You need to tell the target x,y location to swap Itself with the cross sprite tile you want to move. You then need to replace the old cross location with a blank (or previous) tile by calling swap again on the cross tile.

1 Like

How would you tell the X,Y location to swap itself? Would that go in the sprite's script?

It could be done a couple of different ways, but if you want to keep all of the cross tile's movement contained in the cross tile's sprite script you could do something like this:

// first move the cross tile to the new location
tell newX,newY to
    swap "crossTileName"
end

// then delete the cross tile in its current location
swap "backgroundTileName"
1 Like

If you wanted to have the movement happen based on arrow keys being used instead of the crank, what would the event be for that? I can't find an event for the arrows being pressed. I've tried using event.dx etc. but that hasn't worked, presumably because event.dx can only be used in the player's script?

@AdamHopeless check for event.dx and event.dy values in the player tile's update event, then use emit to trigger an event on your target sprite(s).

on update do
    if event.dx==-1 then
        emit "moveSpritesLeft"
   end
   if event.dx==1 then
       emit "moveSpritesRight"
   end
end
2 Likes

@Bro-Code Thank you for taking the time to answer these questions! is there a way to refer to the tile next to the sprite (i.e. SpriteX+/-1) so that the move event can be used for multiples of the same sprite in a room? Or would you need to individually script the movement of each sprite?

I am working from the Pulpscript document but there are some things that either aren't there or that I don't know how to find...

If you're executing the sprites movement from the sprite's own script, it will be executed for every instance of that sprite, so you should be ok. Just note that tile script events execute from top left to bottom right. You may have to do some previous-tile-name storage to avoid undesired overwriting of tiles if they are all moving to the right.

An alternative approach is to create two different, but similar looking tiles. Designate one as the alpha, and have it control all of the other betas. I use this technique with multi-tile "meta sprites". The alpha sprite uses swap to "move" itself and redraw all the other sprites around it. The only catch here is that before you move the alpha sprite, you want to trigger an event on all of the beta sprites to have them "delete" themselves with a swap command.

If you look at the butt-fly movement in the gif below, I'm using that technique to move a 2x2 meta-sprite across the screen.

with buttfly movement

I have multiple instances of a sprite called "Bird" on screen at once. Its script says

on movebirdleft do
swap "white"
tell 17,10 to
swap "Bird"
end
end

on movebirdright do
swap "white"
tell 19,10 to
swap "Bird"
end
end

on movebirdup do
swap "white"
tell 18,11 to
swap "Bird"
end
end

on movebirddown do
swap "white"
tell 18,9 to
swap "Bird"
end
end

This works great for the Bird at 18,10 but doesn't work for all the other ones because when the player moves they just swap themselves for the background tile, disappearing and leaving only one Bird left over. Rather than specifying the tile 18,10 etc. is there a way to automatically refer to whichever tile is immediately adjacent to the sprite's tile?

Calling event.x and event.y references the x,y location of the calling tile.

From your sprite tile you would just assign those to a variable and increment them accordingly.

Here's a crude example. In practice, you'll probably be capturing a variable based on the event.dx and event.dy value and using that to increment the newX and newY variables.

newX = event.x
newX +=  1
newY = event.y
newY += 1

tell newX,newY to
    swap "Bird"
end

I just looked at your code again and realized what you're doing. You need to call swap "white" after you call swap "Bird"

I moved swap "white" to after swap "Bird" and now it doesn't seem to run the script at all, it only runs with swap "white" first. Not sure why that is...

swap "white" needs to be outside of the tell statement.

on movebirddown do
    tell 18,9 to
        swap "Bird"
    end
    swap "white"
end

Ah I see now. Okay that's working, thanks!

Working on the newX and newY now to try and make all the sprites move about. Would
newX = event.x
newX += 1
newY = event.y
newY += 1

tell newX,newY to
swap "Bird"
end

go under each "movebird..." event?

You wouldn't want to increment +1 for each direction, but that would be the general structure. You'll also need to check for the current position to make sure you don't move off-screen.

Something like this:

on movebirdleft do
    // check for min x position
    if event.x != 0
    
        nextX = event.x
        nextX -= 1
        
        tell next.x,event.y to
            swap "Bird"
        end
        
        swap "white"
        
    end
end

on movebirdright do
    // check for max x position
    if event.x != 24
    
        nextX = event.x
        nextX += 1
        
        tell next.x,event.y to
            swap "Bird"
        end
        
        swap "white"
        
    end
end

on movebirdup do
    // check for min y position
    if event.y != 0
    
        nextY = event.y
        nextY -= 1
        
        tell event.x,nextY to
            swap "Bird"
        end
        
        swap "white"
        
    end
end

on movebirddown do
    // check for max y position
    if event.y != 14
    
        nextY = event.y
        nextY += 1
        
        tell event.x,nextY to
            swap "Bird"
        end
        
        swap "white"
        
    end
end
1 Like

Amazing, I'll try that - thanks very much!