0

嗨,我需要有关此代码的帮助。我试图通过动画来让三个球反弹。ii 在 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" (a nil value)

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

4

1 回答 1

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

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

于 2013-07-24T17:27:19.247 回答