Lua:用setAnchorPoint和serRotation创建一个类会显示不寻常的行为



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

我对Gideros相当陌生,并且不完全理解在创建类时需要进行的更改。我真的需要一个简短的解释,好吗?

我正在使用Lua与Gideros

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)

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

如果你旋转了Graphic,那么只有Graphic会旋转,但是如果你旋转了WatchHands,那么它的所有子元素包括Graphic都会旋转。

你可以在ENTER_FRAME事件(如print("Angle:", self:getRotation()))中输入print,看看self的角度是否在增加,或者你的代码中是否出现了其他错误

最新更新