【问题标题】:how to animate a ball in Corona sdk如何在 Corona sdk 中为球设置动画
【发布时间】:2013-07-23 23:21:14
【问题描述】:

您好,我需要有关此代码的帮助。我试图通过动画来使三个球反弹。我在 Corona sdk 示例代码中找到了一些代码,这些代码对我有帮助。我尝试将图像从一个圆圈更改为我文件夹中的图像,但现在幸运的是它不会工作。我也在使用故事板 API 我真的需要这个谢谢我是电晕 sdk 的新手。

这是示例代码

此代码适用于我,但我想添加多个气球,它们会朝自己的方向反弹并相互反弹并改变方向。我有点卡住了,有人可以帮忙吗谢谢:)........

这里是您要求的代码,抱歉花了这么长时间

local function newBall( params )
    local xpos = display.contentWidth*0.5
    local ypos = display.contentHeight*0.5
    local circle = display.newCircle( xpos, ypos, params.radius );
    circle:setFillColor( params.r, params.g, params.b, 255 );
    circle.xdir = params.xdir
    circle.ydir = params.ydir
    circle.xspeed = params.xspeed
    circle.yspeed = params.yspeed
    circle.radius = params.radius

    return circle
end

local params = {
    { radius=20, xdir=1, ydir=1, xspeed=2.8, yspeed=6.1, r=255, g=0, b=0 },
    { radius=12, xdir=1, ydir=1, xspeed=3.8, yspeed=4.2, r=255, g=255, b=0 },
    { radius=15, xdir=1, ydir=-1, xspeed=5.8, yspeed=5.5, r=255, g=0, b=255 },
--  newBall{ radius=10, xdir=-1, ydir=1, xspeed=3.8, yspeed=1.2 }
}

local collection = {}

-- Iterate through params array and add new balls into an array
for _,item in ipairs( params ) do
    local ball = newBall( item )
    collection[ #collection + 1 ] = ball
end

-- Get current edges of visible screen (accounting for the areas cropped by "zoomEven" scaling mode in config.lua)
local screenTop = display.screenOriginY
local screenBottom = display.viewableContentHeight + display.screenOriginY
local screenLeft = display.screenOriginX
local screenRight = display.viewableContentWidth + display.screenOriginX

function collection:enterFrame( event )
    for _,ball in ipairs( collection ) do
        local dx = ( ball.xspeed * ball.xdir );
        local dy = ( ball.yspeed * ball.ydir );
        local xNew, yNew = ball.x + dx, ball.y + dy

        local radius = ball.radius
        if ( xNew > screenRight - radius or xNew < screenLeft + radius ) then
            ball.xdir = -ball.xdir
        end
        if ( yNew > screenBottom - radius or yNew < screenTop + radius ) then
            ball.ydir = -ball.ydir
        end

        ball:translate( dx, dy )
    end
end

Runtime:addEventListener( "enterFrame", collection );

有人可以帮我将文件夹中的图片从圆形更改为我的 balloon01.png 、balloon02.png 和 balloon03.png 图像。这也是我将它添加到包含故事板 API 的游戏时遇到的错误

level1.lua 157:attempt to call global "newBall" (一个 nil 值)

我试图发布一张图片,但因为我是新手,所以我不能。我有在 Corona SDK 故事板 API 之外的创建场景中创建球的代码,感谢 ...:0 的帮助

【问题讨论】:

    标签: android iphone coronasdk corona-storyboard


    【解决方案1】:

    我已经更新了代码。只需尝试用以下代码替换所有代码:

    local xpos,ypos = {},{}
    local xdirection,ydirection = {},{}
    local xMultiplier = {2.8,3.0,4.0}     -- these arrays should contain the values for each objects
    local yMultiplier = {1.0,2.2,5.5}     -- these arrays should contain the values for each objects
    local totalImages = 3     -- no. of images/object that you need in the scene
    local circle = {}
    local diameter = {50,30,20}   -- these arrays should contain the values for each objects
    
    for i=1,totalImages do
        xpos[i] = display.contentWidth*0.5
        ypos[i] = display.contentHeight*0.5
        xdirection[i] = 1
        ydirection[i] = 1
    
        circle[i] = display.newImageRect("balloon0"..i..".png",diameter[i],diameter[i])
        circle[i]:setFillColor(255,0,0,255)
    end
    
    local function animate(event)
        for i=1,totalImages do
          xpos[i] = xpos[i] + ( xMultiplier[i] * xdirection[i] )
          ypos[i] = ypos[i] + ( yMultiplier[i] * ydirection[i] )
    
          if (xpos[i] > display.contentWidth - 20 or xpos[i] < 20) then
              xdirection[i] = xdirection[i] * -1;
          end
          if (ypos[i] > display.contentHeight - 20 or ypos[i] < 20) then
              ydirection[i] = ydirection[i] * -1;
          end
    
          circle[i]:translate( xpos[i] - circle[i].x, ypos[i] - circle[i].y)    
       end
    end
    Runtime:addEventListener( "enterFrame", animate )
    

    注意:确保将以下图像文件放在 main.lua 所在的同一文件夹中:

    1. balloon01.png
    2. balloon02.png
    3. balloon03.png

    继续编码............ :)

    【讨论】:

    • 嘿,感谢您的帮助,但我收到一条新的错误消息,这就是它所说的 level1.ua 8 尝试索引本地圆(“零值”)我查看了本教程,但我发现了一些有效的代码,但是当我尝试添加多个气球时,它们会朝同一个方向反弹,我不知道如何让它们朝自己的方向反弹
    • 请同时发布您的第一个代码(您删除的代码)。
    • 嘿抱歉耽搁了这么长时间我会为你检查并更新代码谢谢
    猜你喜欢
    • 1970-01-01
    • 2021-11-01
    • 2016-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多