Hi everyone! I was wondering if anyone can give me some light on this problem...
So, I have a minigame where the player controls a ship and is able to shoot some bullets and those bullets destroy the enemies he faces.
The idea is simple, I just want to be able to destroy the bullets when they touch the enemy, but for some reason only some bullets get destroyed with my current code... not only that, but the enemies are sometimes taking multiple hits from the same bullet, which I thought wasn't supposed to happen, since they only take damage if the bullet is added to the game...
Here's my code so far:
I'm using the TNT Virtual Pad, so this code right here is activated when the user presses the button:
fireT = CEO_Shot.new(player.xPos + 16, player.yPos + 14)
bulletArray[iBullet] = fireT
self:addChild(bulletArray[iBullet])
bulletArray[iBullet].isDead = false
iBullet = iBullet + 1 |
And this is my onEnterFrame, where I check the collisions:
function CEO_M1:onEnterFrame()
for key,value in pairs(bulletArray)do
print ("key: ", key)
if enemies[0]:hitTestPoint(bulletArray[key]:getX(), bulletArray[key]:getY()) then
if this:contains(bulletArray[key]) then
enemies[0]:takeDamage(5)
bulletArray[key].isDead = true
end
elseif enemies[1]:hitTestPoint(bulletArray[key]:getX(), bulletArray[key]:getY()) then
if this:contains(bulletArray[key]) then
enemies[1]:takeDamage(5)
bulletArray[key].isDead = true
end
elseif enemies[2]:hitTestPoint(bulletArray[key]:getX(), bulletArray[key]:getY()) then
if this:contains(bulletArray[key]) then
enemies[2]:takeDamage(5)
bulletArray[key].isDead = true
end
end
if bulletArray[key]:getX() > self.w then
bulletArray[key].isDead = true
end
end
CEO_M1:killBullets()
end |
This is the code for the killBullets method:
function CEO_M1:killBullets()
for i=0, iBullet-1, 1 do
if this:contains(bulletArray[i]) then
if bulletArray[i].isDead == true then
this:removeChild(bulletArray[i])
iBullet = iBullet - 1
end
end
end
end |
And finally, this is the class from where the bullets are created (C.E.O_Shot):
CEO_Shot = Core.class(Sprite)
function CEO_Shot:init(x, y)
--parametros do minigame
self.h= application:getContentHeight()
self.w= application:getContentWidth()
self.fire = Bitmap.new(Texture.new("images/Minigames/CEO/M1/Pewpew.png"))
self.fire:setAnchorPoint(.5, .5)
self.xPos = x
self.yPos = y
self.fire:setScale(0.8,0.8)
self:setPosition(self.xPos, self.yPos)
self:addChild(self.fire)
--self:setVisible
self:addEventListener(Event.ENTER_FRAME, self.onEnterFrame, self)
end
function CEO_Shot:onEnterFrame(Event)
self.xPos = self.xPos + 180 *Event.deltaTime
self:setPosition(self.xPos, self.yPos)
if self.xPos > self.w then
self:removeEventListener(Event.ENTER_FRAME, self.onEnterFrame, self)
self:removeChild(self.fire)
end
end |
Any help will be deeply appreciated!
Comments
e.g.
if not value.isDead then
for i=1,enemyCount,1 do
if enemies[i]:hitTest...
end
end
Likes: AxlFlame
Also, but it's only about style and ease (not to solve the problem), I'd write something like "local currentBullet = bulletArray[key]" and then deal with the currentBullet variable instead of with bulletArray[key] each time.
Now, I'm not sure I understand what you meant, about using a currentBullet instead of dealing with the bulletArray[key]... what would be the difference? I mean, in both cases you deal with one bullet per for cycle, right?
Likes: Disciple
Yeah it's the same and it's off topic to your question. I just meant for tidiness sake. Personally I prefer something like this but it's the same (sorry for not putting it as code, I don't know how to ) :