It looks like you're new here. If you want to get involved, click one of these buttons!
bDobstaculos = gideros.class(Bitmap); function bDobstaculos:init(text, xPos, yPos, b2World) self:setAnchorPoint(0.5, 0.5); self.body = b2World:createBody{ type = b2.STATIC_BODY, position = {x = 400, y = 140}, fixedRotation = true, allowSleep = false; active = true, gravityScale = 1 }; self.body.name = "spike"; local shapeSpike = b2.PolygonShape.new() shapeSpike:setAsBox(25, 40) self.fixture = self.body:createFixture{shape = shapeSpike, friction = 0, restitution = 0, density = 30, isSensor = true }; self:setPosition(xPos, yPos-self:getHeight()*0.5); end function bDobstaculos:addListeners() self:addEventListener(Event.ENTER_FRAME, self.onEnterFrame, self); self:addEventListener(Event.MOUSE_UP, self.apagaInimigo, self); end function bDobstaculos:removeListeners() self:removeEventListener(Event.ENTER_FRAME, self.onEnterFrame, self); end function bDobstaculos:onEnterFrame() self.body:setPosition(self:getPosition()); local b2World = self.moveMe(self); if self:getX() < -50 then self.fixture = nil; b2World:destroyBody(self.body); self.body = nil; self:removeEventListener(Event.ENTER_FRAME, self.onEnterFrame, self); self:getParent():removeChild(self); end end function bDobstaculos:apagaInimigo() self.fixture = nil; b2World:destroyBody(self.body); self.body = nil; self:removeEventListener(Event.ENTER_FRAME, self.onEnterFrame, self); self:getParent():removeChild(self); end |
Comments
http://www.giderosmobile.com/documentation/reference_manual.html#Sprite:hitTestPoint
if enemySprite:hitTestPoint(event.x, event.y) then
--destroy enemy
end
2.self:getParent():removeChild(self)
you should use:
self:removeFromParent()
Likes: AxlFlame
Ultimate Games on Appstore
Ultimate Games on Google Play
if you want to really make the app 100% proper, then you will have to first check that your touch started on the enemy and the touch ended on the enemy. It might not be important in your app, but what happens if the player start the touch with the friendly unit and then ends it on the enemy? If that is important to you, you might want to also listen for Mouse_Down, not just mouse_up.
Likes: AxlFlame
Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
Cool Vizify Profile at https://www.vizify.com/oz-apps
Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
Cool Vizify Profile at https://www.vizify.com/oz-apps
See, this bDobstaculos.lua is called inside my main game function to create the enemy, but the created enemy is local, so I changed it to global and the code inside the apagaInimigo() is like this:
The point is that this code is giving me this error:
bad argument #1 to 'hitTestPoint' (number expected, got nil)
Why isn't it getting the x and probably the y? Am I on the right track, as least?
@OzApps
I haven't actually thought about that! It sure is important for me that the click starts and ends within the enemy sprite, but I thought it would be best for now to focus on making the MOUSE_UP work, since even that isn't and I didn't quite grasped the idea of the discussion you pointed out...
Check out the "Drag Me" example in the demos. The usual syntax is more like...
Likes: AxlFlame
I did like you said and it finally worked!