Hi there!, I’m failing to see what’s going wrong with this code that I’m sketching for a game.
There are two display groups:
- grupoObjs -> will contain 4 object groups in an horizontal row
- object -> will contain 2 display objects, simple squares with different sizes
A touch handler for the object group that will move the group horizontally in the move event, and will go back to its origin x position in ended event.
The problem is that when the group moved it’s not moving following the cursor (I’m on simulator), but a bit far to the right, and when I release the button it does not go back to it’s origin.
This happens only if I move the entire object group, if I set the touch event to the first display object it works fine. It must be something that I don’t get about the groups so any help is appreciated.
[code]
_W = display.contentWidth
_H = display.contentHeight
display.setStatusBar(display.HiddenStatusBar)
local maxObjs = 4
local objects = {}
local function objectTouchHandler(event)
local target = event.target
if event.phase == “began” then
–target.xScale = 1.1
–target.yScale = 1.1
display.getCurrentStage():setFocus( target )
target.isFocus = true
target.alpha = 0.5
elseif event.phase == “moved” then
if hasCollided(target.boundsToCheck, objects[4]) then
print(“Colision”)
end
target.x = event.x
elseif event.phase == “ended” or event.phase == “cancelled” then
–target.xScale = 1
–target.yScale = 1
target.x = target.origen
print("Target x = "… target.x)
target.alpha = 1
display.getCurrentStage():setFocus( nil )
target.isFocus = false
end
return true
end
– Parent group
local grupoObjs = display.newGroup( )
for i = 1, maxObjs do
local xPos = ( ( _W - 65 ) / maxObjs ) * i
print(“xPos”… i … " = " … xPos )
grupoObjs.x = 0
grupoObjs.y = 100
– To see the reference from the uppermost goup
local xyRefParentGroup = display.newRect(0,0,15,15)
xyRefParentGroup:setReferencePoint(display.CenterReferencePoint)
centerxyRefParentGroupGroup.x = grupoObjs.x
xyRefParentGroup.y = grupoObjs.y
–Object group
local object = display.newGroup()
– 1st display object
local o = display.newRect(object, 0, 0, 50, 50 )
o:setReferencePoint( display.CenterReferencePoint)
o.x = xPos
o.y = 0
grupoObjs:insert(object)
– 2nd display object
local objectCenter = display.newRect(0, 0, 15, 15)
object:insert(objCenter)
objectCenter:setReferencePoint(display.CenterReferencePoint)
objectCenter:setFillColor( 0, 225, 0 )
objectCenter.x = o.x
objectCenter.y = o.y
if i == 4 then
objectCenter.isCollidable = true
else
objectCenter.isCollidable = false
end
object.boundsToCheck = center
object.origen = xPos – X coord to return when ended event is triggered
object:addEventListener( “touch”, objectTouchHandler )
table.insert(objects, o)
end
[/code] [import]uid: 8933 topic_id: 25643 reply_id: 325643[/import]