Center of Sprite

Hi dyson,

I am trying to shoot a laser beam between two sprites on a map.   I can’t seem to get to the center of the sprites, the beam is quite unpredictable.   Using   sprite .x and .y doesn’t find the center of the sprite.  Is there something that I am missing?

I am doing this this:

    local px, py =  mte.locToScreenPos(player.locX, player.locY, 1)
    local px2, py2 = mte.locToScreenPos(k1.locX, k1.locY, 1)
    local beam = display.newLine(px,py, px2, py2)

It works until I move a sprite, then the x and y’s get messed up.

Thanks, Greg

I forgot to mention, when I fire the laser before moving, it works correctly, it’s only after I move my player that the X and Y’s are way off.

Hello gb8,

The first thing that jumps out to me is that you aren’t adding the line to a group managed by the engine. Calling newLine without a parentGroup parameter adds the line to the Stage. Whenever you call moveCamera, moveCameraTo, or the camera follows a sprite, what is actually happening is that the groups containing the map are moving across the Stage. The Stage, meanwhile, remains stationary, and so the x and y coordinates do not match between the two. 

The first thing I would try is to retrieve the group object of the desired map layer by calling mte.getLayerObj({layer = myLayer}). You can then use the returned value as the parentGroup parameter of newLine.

local layerObj = mte.getLayerObj({layer = 1}) local beam = display.newLine(layerObj, player.x, player.y, k1.x, k1.y)

Here is an example, i press on a sprite to the lower right and the line gets drawn correctly. 

I then move the left sprite and then press on the target to the right (which hasn’t moved).

The line origin (player.x and player.y) is not the center of the moved sprite, the destination sprite x and y are correct (but it hasn’t moved)

Thanks again,Greg

I tried adding the code:

  

local layer = mte.getLayerObj({layer = 1})
local beam = display.newLine(layer, px,py, px2,py2)

This might work but I end up with an error:  attempt to perform arithmetic on field ‘offsetX’ (a nil value)  mte.lua:8758

Thanks again, Greg

Aha! You found a bug in the current release! If you go to line 8757 of mte.lua you’ll see the following four lines:

--object.anchorX = ((object.width / 2) - object.offsetX) / object.width object.anchorX = (((object.levelWidth or object.width) / 2) - object.offsetX) / (object.levelWidth or object.width) --object.anchorY = ((object.height / 2) - object.offsetY) / object.height object.anchorY = (((object.levelHeight or object.height) / 2) - object.offsetY) / (object.levelHeight or object.height)

Replace those lines with these lines:

if object.offsetX then object.anchorX = (((object.levelWidth or object.width) / 2) - object.offsetX) / (object.levelWidth or object.width) end if object.offsetY then object.anchorY = (((object.levelHeight or object.height) / 2) - object.offsetY) / (object.levelHeight or object.height) end 

I’ll correct this on my end as well and release it as part of the update on Friday.

excellent!  Phasers at 100% Captain :slight_smile:

I forgot to mention, when I fire the laser before moving, it works correctly, it’s only after I move my player that the X and Y’s are way off.

Hello gb8,

The first thing that jumps out to me is that you aren’t adding the line to a group managed by the engine. Calling newLine without a parentGroup parameter adds the line to the Stage. Whenever you call moveCamera, moveCameraTo, or the camera follows a sprite, what is actually happening is that the groups containing the map are moving across the Stage. The Stage, meanwhile, remains stationary, and so the x and y coordinates do not match between the two. 

The first thing I would try is to retrieve the group object of the desired map layer by calling mte.getLayerObj({layer = myLayer}). You can then use the returned value as the parentGroup parameter of newLine.

local layerObj = mte.getLayerObj({layer = 1}) local beam = display.newLine(layerObj, player.x, player.y, k1.x, k1.y)

Here is an example, i press on a sprite to the lower right and the line gets drawn correctly. 

I then move the left sprite and then press on the target to the right (which hasn’t moved).

The line origin (player.x and player.y) is not the center of the moved sprite, the destination sprite x and y are correct (but it hasn’t moved)

Thanks again,Greg

I tried adding the code:

  

local layer = mte.getLayerObj({layer = 1})
local beam = display.newLine(layer, px,py, px2,py2)

This might work but I end up with an error:  attempt to perform arithmetic on field ‘offsetX’ (a nil value)  mte.lua:8758

Thanks again, Greg

Aha! You found a bug in the current release! If you go to line 8757 of mte.lua you’ll see the following four lines:

--object.anchorX = ((object.width / 2) - object.offsetX) / object.width object.anchorX = (((object.levelWidth or object.width) / 2) - object.offsetX) / (object.levelWidth or object.width) --object.anchorY = ((object.height / 2) - object.offsetY) / object.height object.anchorY = (((object.levelHeight or object.height) / 2) - object.offsetY) / (object.levelHeight or object.height)

Replace those lines with these lines:

if object.offsetX then object.anchorX = (((object.levelWidth or object.width) / 2) - object.offsetX) / (object.levelWidth or object.width) end if object.offsetY then object.anchorY = (((object.levelHeight or object.height) / 2) - object.offsetY) / (object.levelHeight or object.height) end 

I’ll correct this on my end as well and release it as part of the update on Friday.

excellent!  Phasers at 100% Captain :slight_smile: