removeSelf() error please help :)

Hi guys. I really hope you can help. Basically I want my rectangle "j_1" to remove it self from the screen when it collides with "side". I keep getting a error. Can anyone please help me :)

Thanks I am greatly thankful.

Regards,
Jordan Schuetz
Ninja Pig Studios

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
45
46
47
48
49
50
51
52
53
54
55
56
module(..., package.seeall)
 
_W = display.contentWidth;
_H = display.contentHeight;
require "sprite"
require "ui"
local physics = require("physics")
physics.start()
physics.setScale(5 )
physics.setGravity( -9.8, 0 )
function new()
        local localGroup = display.newGroup();
        
        side = display.newRect(0,0,20,500)
        side:setFillColor(140, 140, 140)
        side.x = 1
        physics.addBody(side, "static")
        
function newRect()  
 
        j_1 = display.newRect(0, 0, 25, 25)
              j_1:setFillColor(140, 140, 140)
              j_1.x = _W/1 + 50
              j_1.y = _H/math.random(1.2, 8)
              physics.addBody(j_1)     
              j_1.collision = onLocalCollision
              j_1:addEventListener( "collision", j_1 )
                
               
end
 
  local function onLocalCollision( self, event )
                                        if ( event.phase == "began" ) then
                                                newRect:removeSelf()
                                        end
                                end
local spawn_tmr = timer.performWithDelay( 500, newRect, 10 )
 
 
 
 
 
--localGroup:insert(j_1)
localGroup:insert(side)
 
        function changeScene(e)
                if(e.phase == "ended") then
                        director:changeScene(e.target.scene, "fade");
                end
        end
        
        side:addEventListener( "collision", side )
        side.collision = onLocalCollision
return localGroup
        
end

try this...

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
_W = display.contentWidth;
_H = display.contentHeight;
local physics = require("physics")
physics.start()
physics.setScale(5 )
physics.setGravity( -9.8, 0 )
 
        local localGroup = display.newGroup();
        
        side = display.newRect(0,0,20,500)
        side:setFillColor(140, 140, 140)
        side.x = 1
        physics.addBody(side, "static")
        
function newRect()  
 
        j_1 = display.newRect(0, 0, 25, 25)
              j_1:setFillColor(140, 140, 140)
              j_1.x = _W/1 + 50
              j_1.y = _H/math.random(1.2, 8)
              physics.addBody(j_1)     
              j_1.collision = onLocalCollision
              j_1:addEventListener( "collision", j_1 )             
end
 
 local function onLocalCollision(event )
             local otherObject = event.other
         otherObject:removeSelf()
end
 
local spawn_tmr = timer.performWithDelay( 500, newRect, 10 )
 
side:addEventListener("collision", onLocalCollision)        

OMG! Thank you so much! It worked! I'm very thankful for your time and effort to fix this problem.

Regards,
Jordan Schuetz
Ninja Pig Studios

you are welcome....happy coding...

views:1689 update:2011/10/8 8:54:25
corona forums © 2003-2011