Sprites drawn on top of each other move together

I have some globals:
BombIsSet = false bombs = 0 bx = 0 — player starting x pos by = 0 — player starting y pos
And I have a Bomb class:
` Bomb = class()

function Bomb:init(x, y)
    self.clock = ElapsedTime
    self.x = x
    self.y = y
    self.bombRad = 0
end

function Bomb:draw(ox, oy)
    if ElapsedTime - self.clock < 3.0 then
        sprite(asset.Bomb_1, self.x, self.y + 16, 32, 32)
    else
        sprite()
        self.bombRad = self.bombRad + 1
        noStroke()
        fill(125, 125, 125, 255 - self.bombRad * 10)
        ellipse(self.x, self.y, self.bombRad * 15)
        if self.bombRad > 60 then
            self.bombRad = 0
            bombIsSet = false
        end
    end
end `

And a player sprite that moves around. The player is drawn first and the bomb if the bomb button is touched. They are both drawn at the same coordinates. The player should move away before the bomb goes off. The bomb class code seems to function properly. Drop a bomb, 3 seconds pass and it explodes. The problem is when the player moves, the bomb moves with the player 90% of the time.

The bomb touch code (within the touch function):

elseif bombButton:touched(touch) and bombs > 0 then if not bombIsSet then b = Bomb(bx, by) bombIsSet = true bombs = bombs - 1 end end

The bomb draw code (within the draw function):
if bombIsSet then b:draw(self.ox, self.oy) end

The draw parameters are unused as exemplified in the bomb class code. Is there something simple that I missed that would cause the two drawn images appear to move together instead of the bomb staying put? I have been racking my brain and I keep thinking its a timing issue. bx and by change as the player moves and the x and y values don’t change. Thanks in advance!

@krdavis Theres not much code there for me to see what’s really going on. You’re setting the player position to bx, by. In the bombButton:touched function, you’re setting b = Bomb(bx,by) which is the player position. Then you’re saying the bomb draw code is b:draw(self.ox,self.oy). So b is an instance of Bomb and the b draw is using the player position. So the bomb is using the player values. Then again there’s not enough code to see what’s really happening.

Here is the complete project

Where does the bomb come from

Just a quick note, any image/sprite you use that’s not included in Codea can’t be seen by anyone but you unless you include them.

They were internal assets that I added via the asset manager, I thought the export would handle that. Sorry I am a noob with the tool.

Here’s the bomb:

No problem, I added a sprite from Codea in its place. What I’m seeing is what I said above, the bomb is being set to bx, by and as you move the player, the bomb move too. I’m still trying to figure out your code, but it looks like you want the initial bomb position to be the player position, but not updated as the player moves.

Correct!

You have a lot of code to look thru, and I’ll be turning in for the night soon.

No prob, grateful for the help…

OK, here what I found so far. You set the bomb to the player position, but as you move the player you’re not moving the bomb with the background grid.

Ok

I tried my screenMap() in Bomb like I do with treasure and monster, no luck…

It’s 10:50, I’m off to bed in a few minutes. I’ll look at it more tomorrow.

1 - Where are bx and by coming from? I don’t see them globally defined in the main file. You should be passing cx and cy.
2 - in Bomb file draw you need this line that accounts for the way the “camera” or rather tile sliding works in this game:
local x, y = screenMap(self.x, self.y)
and use x and y in sprite and ellipse where you had self pointers before

Thanks @skar and @dave1707, that worked. I swear I tried that before… But I must not have passed cx, cy to the Bomb init() function, that was the fix. @skar bx, by is defined in the floor object draw() function. Mine was a case of taking away and adding too many things while trying to debug this…

@krdavis i see, so the reason bx and by don’t work is a bit complicated but trying to simplify it:
The character both moves along the grid and the grid moves when the character is pushing towards the edge of the screen. When you use bx and by you are telling the bomb to place itself at those current character coordinates. When the tiles shift the bomb stays in place because bx and by are still relevant to the character position and what you want is actually a new position based on how the tiles shifted, this is what screenMap does

It’s why you were seeing this issue “90%” of the time, it happened when you were moving the tiles and doesn’t happen when you move the character (when they have enough screen space to change grid position without shifting all the tiles)

Correct! Took me a while to realize that…