Sprite hitting wall

Can someone please help me with this question?

In a tile map, I have sprites moving left and right of the screen. When a sprite hits the boundary of the map, I place them onto the other side and set their speed again. This occurs within an onHitWall event:

scene.onHitWall(SpriteKind.Log, function (sprite, location) {
placeEnemyAgain(sprite, location)

})

function placeEnemyAgain (anEnemy: Sprite, location: tiles.Location) {

// this is always a positive/negative number
aSpriteSpeed = sprites.readDataNumber(anEnemy, dataSpriteSpeed)

// the row where the sprite started
aSpriteRow = sprites.readDataNumber(anEnemy, dataRow)

// assumming that a sprite is moving right, place the sprite at the first column
aSpriteColumn = 0

// change the column if the sprite is moving left
if (aSpriteSpeed < 0){
    aSpriteColumn = totalColumns - 1
}

// switch sides of the screen
grid.place(anEnemy, tiles.getTileLocation(aSpriteColumn, aSpriteRow))

// set the speed, just in case the sprite stopped
anEnemy.vx = aSpriteSpeed

}

The sprite switches sides, but no longer moves. I suspect this has to do with the events of the game loop. I think the wall event fires independently of the game loop, and that collision detection happens later causing the sprite to stop.

Is there a different event I need to use to keep the sprites moving? Can I better manually check within the game update loop, that if a sprite is nearing the edge, to place it on the other side, also a 1 pixel away from the edge? If so, I am not sure how to calculate the x,y positions in regard to a tile map.

Hm, so is this the behavior you’re looking for?

It’s not behaving quite as you’re describing – on wall hit events occur synchronously within the game loop. This issue is that the event isn’t a substitute for the default behavior of hitting a wall – you wouldn’t typically want it ignore the behaviors of being a wall (i.e. stopping a sprites movement in that direction and preventing the sprite from moving through it). That said, we still want to allow for cases like making the sprite bounce back and forth (i.e. bounce on walls should be implementable with on wall hit), so there’s a special case to not override the velocity when the velocity changes in the wall hit event – so this works

(which is why the first link works; I changed the vx just enough that it compares as ‘not equal’ to the previous speed)

I can see adding another special case for when the position changes in the handler as well, but have to think about it a bit more, as any sort of special case like that is something we have to consider / as with all special cases someone might run into it and expect the normal behavior in the future and be confused by it. Could make for cute little ‘animations’ though

2 Likes

Hi Joey,
I appreciate you going to great lengths to explain various possible options, but all involve changing the speed of the sprite when it hits the wall; which is not ideal in what I am trying to achieve. In the end, I keep track of all sprites hitting the wall, place it on the other side, and in a ‘game update interval’ I go through this array and set them in motion again. Maybe not ideal but I am ok with the result.