0
votes

When i try a collision with BCloud1.png bed i get a attempt to index global 'BCoud1' (a nil value) ? is there any simple ways to resolve this error ?

i have done this :

function getRandomStar()
    local temp = starTable[math.random(1, #starTable)] -- Get a random star from starTable
    local randomStar = display.newImage(temp.imgpath) -- Set image path for object

    if ( temp.imgpath == "BCloud1.png" ) then
    temp.imgpath = "BCloud"..tostring(math.random(1, 3))..".png";
    physics.addBody( BCloud1, { density=3.0, friction=0.5, bounce=0.3 } )
    end

    if ( temp.imgpath == "BCloud2.png" ) then
    temp.imgpath = "BCloud"..tostring(math.random(1, 3))..".png";
    physics.addBody( randomStar, "static", { density=.1, bounce=.1, friction=.2, radius=45 } )  
    end

    if ( temp.imgpath == "BCloud3.png" ) then
    temp.imgpath = "BCloud"..tostring(math.random(1, 3))..".png";
    physics.addBody( randomStar, "static", { density=.1, bounce=.1, friction=.2, radius=45 } )  
    end

    randomStar.myName = "star" -- Set the name of the object to star
    randomStar.movementSpeed = temp.movementSpeed; -- Set how fast the object will move
    randomStar.x = math.random(-30, _W);    
    randomStar.y = -35;
    randomStar.rotation = math.random(0,20) -- Rotate the object

    starMove = transition.to(randomStar, {
        time=randomStar.movementSpeed, 
        y=500,
        onComplete = function(self) self.parent:remove(self); self = nil; 
        end
        }) -- Move the star
end--END getRandomStar()


local function onLocalCollision1( self, event )
    if ( event.phase == "began" ) then
print (" Collision1 ")
    elseif ( event.phase == "ended" ) then
    end
end
BCloud1.collision = onLocalCollision1
BCloud1:addEventListener( "collision", BCloud1.png)
1
Check your parameters for physics.addBody. BCloud1 is initialized with probability 1/3.Egor Skriptunoff
yes but if it is 1 i want to add physics.addBody( BCloud1,kevin ver
You should frame operations with BCloud1 with if BCloud1 then ... endEgor Skriptunoff

1 Answers

1
votes

Where do you create a display object called BCloud1?

In your code:

BCloud1.collision = onLocalCollision1
BCloud1:addEventListener( "collision", BCloud1.png)

BCloud1 needs to exist and your posted code never creates this. I suspect it starts in your getRandomStar() function. You never return the created object. But also in your eventListener setup above, you are treating BCloud1.png like a table called BCloud1 with member called .png, though looking at it, BCloud1.png sounds like the file name of an image. Maybe you're missing something like:

BCloud1 = display.newImage("BCloud1.png")