我有一个海狸,意在跟随用户frog
。它以较慢的速度向青蛙的方向移动。但是我希望海狸有一个左右动画。所以我让运动起作用,而不是动画。
local BidoofSheetData =
{
width = 32,
height = 48,
numFrames = 8,
sheetContentWidth = 128,
sheetcontentheight = 96
}
--Set File Actual size
bidoofSheet = graphics.newImageSheet ("BidoofSpriteSheet.png", BidoofSheetData)
--Set the sequences
local bidoofsequenceData = {
{name = "bstop", start = 1, count = 1, time = 300},
{name = "bleft", start = 2, count = 3, time = 300},
{name = "bright", start = 5, count = 3, time = 300}
}
--frog mask
local physicsData = (require "bidoofdefs").physicsData(1.0)
--Link sheet data to previous settings
beaver = display.newSprite(bidoofSheet, bidoofsequenceData)
beaver.x = display.contentWidth/2
beaver.y = 284
physics.addBody( beaver, "static")
beaver.isFixedRotation = true
--
function moveBeaver ()
if frog.x > beaver.x then
beaver.x = beaver.x + 0.5
elseif frog.x < beaver.x then
beaver.x = beaver.x - 0.5
elseif frog.x == beaver.x then
beaver.x = beaver.x
end
end
Runtime:addEventListener("enterFrame", moveBeaver)
我尝试在moveBeaver
函数中添加它,但它不起作用。
编辑:
我尝试将beaver:setSequence("bleft");beaver:play()
添加到不同的区域。如果你朝各自的方向移动,它会为左边和右边一个框架。如果你向左或向右移动并停止,它将不断播放左右两帧。
但它没有播放我想要的3帧动画。
答案 0 :(得分:1)
首先,我注意到您的工作表数据不一致。
local BidoofSheetData =
{
width = 32,
height = 48,
numFrames = 8,
sheetContentWidth = 128,
sheetcontentheight = 96 -- Shouldn't this be sheetContentHeight ?
}
我不确定sheetContentHeight
是否具有适当的大写字母,但我认为我会把它提起来。我想我现在知道动画的问题。您将它设置为在海狸需要移动时播放,这会将其重置为动画的第一帧。
请改为尝试:
function updateAnim(who, seq)
if who.sequence == seq then
-- We're already animating the way we need to be.
return
end
who:setSequence(seq)
who:play()
end
function moveBeaver()
-- Get the distance from beaver to frog's position.
local d = frog.x - beaver.x
-- This will allow the beaver to stop precisely on the frog's position,
-- without exceeding a distance of +/- 0.5 per move.
if d == 0 then
updateAnim(beaver, "bstop")
elseif d > 0 then
beaver.x = beaver.x + math.min(d, 0.5)
updateAnim(beaver, "bright")
else
beaver.x = beaver.x + math.max(d, -0.5)
updateAnim(beaver, "bleft")
end
end