Lua:使用setAnchorPoint和serRotation创建一个类显示异常行为

时间:2014-05-04 19:41:59

标签: lua gideros

我有这段代码。在这个例子中,如果我替换' Graphic'与自我'在Graphic:setAnchorPoint和Graphic:setRotation,图像出现在屏幕上但不旋转。是什么原因?

我对Gideros相当新,并且不完全理解我在创建课程时需要做出的改变。真的需要简短的快速解释,好吗?

我正在和Gideros一起使用Lua

WatchHands= Core.class(Sprite)
W, H = application:getDeviceWidth(), application:getDeviceHeight() 
H, W = W, H
ori = Application.LANDSCAPE_LEFT
application:setOrientation(ori)


function WatchHands:init(Image, posx, posy)
posx = posx or 0
posy = posy or 0
Graphic = Bitmap.new(Texture.new(Image))
Graphic: setAnchorPoint(0.2257, 0.5)
self:addChild(Graphic)
self: setPosition( posx, posy)
self.width = self: getWidth()
self.height = self: getHeight()
self: setScale(0.5, 1) 
Graphic:setRotation(math.random( 1,360))
self:addEventListener(Event.ADDED_TO_STAGE, self.onAddedToStage, self)
return self
end

function WatchHands: playsound(sound)
local channel = sound:play()
return channel
end 
function WatchHands: onAddedToStage()
self:addEventListener(Event.ENTER_FRAME, 
   function()
       self:setRotation( Graphic:getRotation() + 5)
        Timer.delayedCall(math.random(30000, 60000), 
            function()
                self:setRotation( self:getRotation() + math.random(6,10) )
            end)
    end)
    end
sechand = WatchHands.new("secondshand.png", W/2, H/2)
minhand = WatchHands.new("minutehand.png", W/2, H/2)

 stage: addChild(sechand)
 stage: addChild(minhand)

1 个答案:

答案 0 :(得分:1)

在您的情况下,Graphic是您要操作的Bitmap的实例,而self正在引用您所在的类的实例,在您的情况下为WatchHands

如果你旋转Graphic,那么只有Graphic应该旋转,但如果你旋转WatchHands,那么包括Graphic在内的所有孩子都应该旋转。

您可以在ENTER_FRAME事件中添加打印内容,例如print("Angle:", self:getRotation()),以查看自我的角度是否实际增加,或者您的代码中是否有其他错误

相关问题