如何在Corona sdk中设置球的动画



hi我需要这段代码的帮助。我正试图通过动画使三个球弹跳。我在Corona sdk示例代码中发现了一些代码,这些代码对我有所帮助。我试着把图片从一个圆圈改成我文件夹里的图片,但现在运气不好。此外,我正在使用故事板API我真的需要这个感谢我是新的coronasdk。

这是的样本代码

这段代码对我有效,但我想添加多个气球,它们会朝自己的方向反弹,相互反弹并改变方向。我有点纠结于这个问题。有人能帮忙吗?谢谢:)。。。。。。。。。。。。。。。。。。。。

这是你要的代码,抱歉花了这么长时间

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 );

有人能帮我把我文件夹里的图片从圆形改成气球01.png、气球02.png和气球03.png吗。这也是当我把它添加到我的游戏中时,我得到的错误,其中包括故事板API

level.lua157:尝试调用全局"newBall"(零值)

我试图发布一张图片,但因为我是新手,所以我做不到。我有在创建场景中装球的代码,这是Corona SDK故事板API的一部分,谢谢…:0请求您的帮助

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

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. 气球01.png
  2. 气球02.png
  3. 气球03.png

保持编码…………:)

最新更新