首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在if语句中使用spritesheet移动图像

在if语句中使用spritesheet移动图像
EN

Stack Overflow用户
提问于 2013-11-25 17:00:24
回答 1查看 125关注 0票数 1

我有一个海狸,它是用来跟踪用户frog的。它以较慢的速度向青蛙的方向移动。然而,我希望海狸有一个左右动画。所以我的动作起作用了,而不是动画。

代码语言:javascript
复制
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帧动画。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-11-25 17:47:51

首先,我注意到您的工作表数据不一致。

代码语言:javascript
复制
local BidoofSheetData = 
{   
    width = 32,
    height = 48,
    numFrames = 8,
    sheetContentWidth = 128,
    sheetcontentheight = 96 -- Shouldn't this be sheetContentHeight ?
}

我不确定sheetContentHeight的大写是否合适,但我想我会提出来。我想我现在知道你的动画有什么问题了。您将其设置为在海狸需要移动的任何时候播放,这会将其重置为动画的第一帧。

试一试:

代码语言:javascript
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20198836

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档