Do you happen to know a way to use sprite sheets and display objects in this type of class? I setup a variable called enemySprite that holds the sprite, but now on collision events e.other refers to the enemySprite and not the enemy class itself. I’m not sure if there is a way to reference the main object from e.other or if i am way off in doing this anyway. Here is some of my code:
This is a different class than the one in the question above btw (obviously this isn’t then entire class, but hopefully it’s enough for you to go on).
[code]
–
– enemy.lua
–
local enemy = {}
local enemy_mt = { __index = enemy } – metatable
– PRIVATE FUNCTIONS
– PUBLIC FUNCTIONS
function enemy.new( displayGroup, name ) – constructor
– Load sprite sheet data and prepare sprite
local enemyData = require(“resources.sprite_sheets.guy”)
local enemySpriteSheet = sprite.newSpriteSheetFromData( “resources/sprite_sheets/guy.png”, enemyData.getSpriteSheetData() )
local enemySpriteSet = sprite.newSpriteSet(enemySpriteSheet, 1, 24)
sprite.add(enemySpriteSet,“run”, 1, 10, 1000, 0)
sprite.add(enemySpriteSet,“walk”, 11, 14, 1000, 0)
local enemySprite = sprite.newSprite(enemySpriteSet)
– Insert cat into display group
–displayGroup:insert(enemySprite)
– Prepare animation and play
enemySprite:prepare(“walk”)
enemySprite:play()
– Add physics body
physics.addBody(enemySprite, “kinematic”, { isSensor = true })
enemySprite:setLinearVelocity(1000)
– Position cat
enemySprite.x = -(enemySprite.width)
enemySprite.y = 312 - (enemySprite.height * 0.5)
enemySprite.collisionCount = 0
local newEnemy = {
enemyTableId,
objType = “enemy”,
name = name or “Unnamed”,
active = false,
collisionCount = 0,
enemyData = enemyData,
enemySpriteSheet = enemySpriteSheet,
enemySpriteSet = enemySpriteSet,
enemySprite = enemySprite,
destroyTimer
}
return setmetatable( newEnemy, enemy_mt )
end
function enemy:move(velocity)
– Add collision listener
self.enemySprite:addEventListener(“collision”, self)
– Set velocity
self.enemySprite:setLinearVelocity(velocity)
self.setDestroyTimer = timer.performWithDelay(10, function() – Wait 1 frame
if self.enemySprite.x > 0 then
timer.cancel(self.setDestroyTimer)
self:destroyTimer()
end
end, 0)
end
return enemy
[/code] [import]uid: 26491 topic_id: 30357 reply_id: 121715[/import]