Drop Boxes from Airplane

Hello Community, I’m trying to my airplane drops boxes between a specific range. Here’s my code:

[code]
local airplane
local crate
local airon = false
local c

local function spawnair ()
airplane = display.newImage ( “Airplane.png” )
airplane:setReferencePoint ( display.TopCenterReferencePoint )
airplane.x = -50
airplane.y = 50
airon = true
c = mRandom ( 150, 460 )
end
timer.performWithDelay ( 4000, spawnair, 0 )

local function cra ()
if ( gameplay == true and airon == true ) then
if ( airplane.x == c ) then
crate = display.newImage ( “Crate.png” )
crate:setReferencePoint ( display.TopCenterReferencePoint )
crate.x = airplane.x
crate.y = 70
physics.addBody ( crate )
end
end
end

Runtime:addEventListener ( “enterFrame”, cra )

local function air ()
if ( gameplay == true and airon == true ) then
airplane.x = airplane.x + 10
end
end

Runtime:addEventListener ( “enterFrame”, air )

local function remair ()
if ( gameplay == true and airon == true and airplane.x == 530 ) then
airplane:removeSelf ()
airplane = nil
airon = false
end
end
[/code] [import]uid: 81091 topic_id: 21295 reply_id: 321295[/import]

Hey there,

What problem are you actually having with your code?

You want to drop in a specific range but you are only saying once box should be dropped when the airplane.x = c.

Are you wanting a range or a single point where it drops? [import]uid: 52491 topic_id: 21295 reply_id: 84375[/import]

I think when the airplane.x is between these values ( 150, 460 ) a crate will drop. [import]uid: 10389 topic_id: 21295 reply_id: 84418[/import]

I think your problem is you are incrementing airplane.x by 10 each frame, but your ‘c’ value could be 132, 263, 312 or whatever which will never match your airplane.x value.

You could change it to:

[lua]c = mRandom(15,46) * 10 [/lua]

…so that ‘c’ will always be a value that will match airplane.x at some point.
Or change the condition to:

[lua]if ( gameplay == true and airon == true and dropped == false ) then
if ( airplane.x > c ) then
dropped == true [/lua]

…so that as soon as the airplane moves past ‘c’ it will drop the crate, and then set dropped to true so it won’t drop again. [import]uid: 93133 topic_id: 21295 reply_id: 84421[/import]

the FCC doesn’t allow cell phones to be on during flight so you can’t connect to Internet to reach Dropbox
sorry couldn’t resist [import]uid: 7911 topic_id: 21295 reply_id: 84431[/import]

@Jstrahan - you may be onto something… Alec Baldwin springs to mind :wink:

Nick’s post is spot on; the airplane right now would only drop at “c” which it may not ever match with your current setup. [import]uid: 52491 topic_id: 21295 reply_id: 84485[/import]

@nicksherman - Yeah! You’re right… I changed the values from mRandom ( 15, 46 ) * 10, and goes perfect…

Thank you everyone! [import]uid: 81091 topic_id: 21295 reply_id: 84530[/import]