0
votes

hi i need help with this code. i am trying to make three ball bounce around by animating it. i i found some code that code be some help in the Corona sdk sample code. i try to change the image from a circle to a image i have in my folder but now luck it wont work. also i am using story board API i really need this thanks i am new to corona sdk.

this is the sample code

this code works for me but i want to add multiple balloons the would bounce around in their own direction and bounce in to each other and change direction. I am kinda stuck on this can someone help thanks :)....................

here the code you ask for sorry for taking so long

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

can someone help me to change the images from circle to my balloon01.png ,balloon02.png and balloon03.png images in my folder. also this is the error i get when i add it to my game that include the story board API

level1.lua 157:attempt to call global "newBall" (a nil value)

i was trying to post an image but because i am new i can't. I have the code that crate the ball in the create scene that's apart of the Corona SDK story board API thanks ...:0 for your help

1

1 Answers

1
votes

I've updated the code. Just try replacing all your code with the following code:

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 )

Note: Make sure to place the following image files in the same folder where your main.lua resides:

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

Keep Coding............ :)