Drop Boxes from Airplane

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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

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?

I think when the airplane.x is between these values ( 150, 460 ) a crate will drop.

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:

1
c = mRandom(15,46) * 10 

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

@Jstrahan - you may be onto something... Alec Baldwin springs to mind ;)

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.

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

Thank you everyone!

views:1456 update:2012/2/8 8:46:10
corona forums © 2003-2011