【发布时间】:2014-01-04 13:04:36
【问题描述】:
我有一个像这样的精灵,在一个类中实例化,其中包括一个使用texture packer 生成的文件,其中包含所有精灵:
local sheetInfo = require("db.sprites." .. self.element["name"])
self.sheet = graphics.newImageSheet( "db/sprites/" .. self.element["name"] .. ".png", sheetInfo:getSheet() )
self.sprite = display.newSprite( self.sheet , element["animations"] )
self.sprite.xScale, self.sprite.yScale = 0.3, 0.3
现在,我的目标是动态生成一个适合精灵的物理矩形。在正常情况下,这很简单。我的代码是:
function defineBody(sprite)
if (sprite.xScale == 1 and sprite.yScale == 1) then return nil end
local shape = {}
local wx, wy = sprite.width * sprite.xScale/2, sprite.height * sprite.yScale/2
local nwx, nwy = sprite.width * sprite.xScale/-2, sprite.height * sprite.yScale/-2
shape[1],shape[2] = nwx, nwy
shape[3],shape[4] = wx, nwy
shape[5],shape[6] = wx, wy
shape[7],shape[8] = nwx, wy
return shape
end
然后我这样调用这个函数:
physics.addBody( self.sprite, self.element["physics"], { friction=0.5, bounce=0.3, shape=defineBody(sprite)} )
结果:
我的问题是:
当图像的锚点也发生变化时,如何更改该功能以使其正常工作?示例:
self.sprite.anchorX, self.sprite.anchorY = 0.5, 1
我尝试了很多,但我没有找到一个简单的方法来实现它。
【问题讨论】:
标签: graphics lua box2d coronasdk game-physics