Question about setGravity

Hi!

Question:

I have a regular gravity set like this:

[lua]fisica.setGravity(0,0.5)[/lua]

I also have some balls on the screen, and when one of them collides with an object, I modify the gravity to “negative”, like this:

[lua]fisica.setGravity(0,-0.5)[/lua]

Once I change the gravity to negative, I should go to a different scene, but when I do that, an object from the previous one suddenly “appears” on it (on the new scene).

The strange thing it’s that if I use “setGravity” as “positive”, this object doesn’t appear in the new scene!

What I’m missing here?

Thanks!

Regards.

How do you add object (which “appear” on new scene) to scene display group? 

That’s just a red-herring, correlation without causation…  i.e. This has nothing to do with gravity.  In all likelihood the only actual correlation is that your gravity change is ensuring the object is in a position where you see it after the change.

The real problem is that your object(s) are not in the scene group or a child of the scenegroup.  If they are not ‘owned’ by the scene group it won’t manage them.

Check you code for places where you’re not inserting objects into the scene group or a child.

Yes, that is probably the solution!

But my problem is that the “object” is being managed as local in a function, and when I use:

[lua]sceneGroup:insert(object)[/lua]

it gives me this error:

scene.lua:444 ERROR: table expected. If this is a function call, you might have user “.” instead of “.”

How could I avoid this?

Thanks!

Regards.

You’ve got a scoping issue.  Resolve that and you’re golden.  However, you must understand Lua’s scoping rules (not a Corona thing).

In this particular case ‘sceneGroup’ is not visible in the scope you’re trying to use it.  You need to solve that problem.  

How, is up to you and strongly affected by the way you’ve architected your particular game.  I can’t really give advice without seeing the entire thing. 

Thanks, I know that you are right but this is really driving me crazy!

Here is a sample of this piece of code:

[lua]–scene

– this object should appear when aa ball collides with an object, blink, and finally dissapear
local objectCross

– function where I use the fadeOut
local function disappearObjectCross()
    objectCross.isVisible = true
    transition.fadeOut( anotherFunction, { time = 1000 } )
    objectCross = nil
end

local function ballHit()
 if (ball ~= nil) then
    ball:addEventListener(“collision”,sound)
     ball:removeSelf()
    disappearObjectCross()
 end
end

– code…

function scene:create( event )

    local sceneGroup = self.view
    – Code here runs when the scene is first created but has not yet appeared on screen
    
    objectCross = display.newImageRect(“objectimage.png”,100,80)
    objectCross.x = 275
    objectCross.y = 165
    objectCross.isVisible = false
    sceneGroup:insert(objectCross)
    
    – code…
end

– code …[/lua]

Where I am wrong in here?

Thanks!

Regards.

I don’t really understand what your code is doing, but I can see you’re not following a good model for object creation and managment.
 
You’re much better off writing a factory function (builder) and using table listeners instead of a file level variable and standalone functions.
 
This example may have typos and may not do what you want, but focus on the ‘model’ I used to create and manage the object as well as its listeners:
 

local function sound() -- You showed this in your code, but didn't provide it. -- This is a placeholder for the example below. -- Your version must be defined BEFORE createBall() end local function createBall( group, x, y ) -- Tip: Double check the case of the file "objectimage.png" -- It it does not match this will fail on devices local ball -- display.newImageRect( group, "objectimage.png", 100, 80 ) ball.x = x ball.y = y function ball.collision( self, event ) if( event.phase == "began" ) then self:removeEventListener("collision") sound() local function onComplete() display.remove( self ) -- I NEVER use obj:removeSelf() end transition.to( self, { alpha = 0, time = 1000, onComplete = onComplete } ) end return false end ball:addEventListener("collision") return ball end function scene:create( event ) local sceneGroup = self.view local ball = createBall( sceneGroup, 275, 165 ) end

Again, may be totally missing the point of your prior code post… Hope that helped.

Hi again! I got a little confused with your code, although it is clearly more efficient than mine.

What I need to do is something like this:

Once a “ball” collides with an object (let’s say a bar), a red cross should appear for one second, fading out until it “dissapear” (indication of a mistake).

I have several “balls” that will collide with this “bar”, and every time this happens, the object “red cross” should appear and fade out in one second.

The only way I found to solve this was using a “local object redCross” inside the function.

This works perfectly, but once I collide the “right ball” with the “bar”, I should change the scene and return to the previous one.

Once I see the previous one, the object “red cross” magically appears and disappear again.

I guess this is because of the fact that my “red cross” is not added to the scene, cause it’s a local variable in a function, right?

I tried adding the “function that contains the red cross local object” in the scene, but it gives me an error, and/or I couldn’t know how to do it.

Perhaps now it’s more clear what I need.

Thanks again!!

I am still not clear on what you’re trying to do.

So I made up a example that demonstrates one way to handle scope and visibility in composer scenes.

http://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2016/10/redcross.zip

Be aware, this example uses my custom composer scene which splits show/hide into willShow/didShow and willHide/didHide events.

I hope by looking at what I’ve done here you can get a better understanding of how scope and visibility works in composer scenes, and that with that info you can proceed.

OK I’ll take a look at your project.

Thanks!

Hi again! I was checking your code and it is really good!

What I need to do in this case is:

Instead of going from scene2 to scene1 when I “touch” the screen,

I need to do that when any ball (8 balls in total) collides with an invisible object (in this the “cross” should appear indicating a mistake, and continue in the scene).

Should I user “onCollision” function instead of “onTouch” function?

Sorry but this is the most confusing part for me, and is the only thing I couldn’t solve yet in order to finish it.

Thanks again!!!

Not exactly, I only need a cross that appears when a ball wronlgy collides with an object, and the cross should disappear after 2 seconds for example, blinking in the process.

How do you add object (which “appear” on new scene) to scene display group? 

That’s just a red-herring, correlation without causation…  i.e. This has nothing to do with gravity.  In all likelihood the only actual correlation is that your gravity change is ensuring the object is in a position where you see it after the change.

The real problem is that your object(s) are not in the scene group or a child of the scenegroup.  If they are not ‘owned’ by the scene group it won’t manage them.

Check you code for places where you’re not inserting objects into the scene group or a child.

Yes, that is probably the solution!

But my problem is that the “object” is being managed as local in a function, and when I use:

[lua]sceneGroup:insert(object)[/lua]

it gives me this error:

scene.lua:444 ERROR: table expected. If this is a function call, you might have user “.” instead of “.”

How could I avoid this?

Thanks!

Regards.

You’ve got a scoping issue.  Resolve that and you’re golden.  However, you must understand Lua’s scoping rules (not a Corona thing).

In this particular case ‘sceneGroup’ is not visible in the scope you’re trying to use it.  You need to solve that problem.  

How, is up to you and strongly affected by the way you’ve architected your particular game.  I can’t really give advice without seeing the entire thing. 

Thanks, I know that you are right but this is really driving me crazy!

Here is a sample of this piece of code:

[lua]–scene

– this object should appear when aa ball collides with an object, blink, and finally dissapear
local objectCross

– function where I use the fadeOut
local function disappearObjectCross()
    objectCross.isVisible = true
    transition.fadeOut( anotherFunction, { time = 1000 } )
    objectCross = nil
end

local function ballHit()
 if (ball ~= nil) then
    ball:addEventListener(“collision”,sound)
     ball:removeSelf()
    disappearObjectCross()
 end
end

– code…

function scene:create( event )

    local sceneGroup = self.view
    – Code here runs when the scene is first created but has not yet appeared on screen
    
    objectCross = display.newImageRect(“objectimage.png”,100,80)
    objectCross.x = 275
    objectCross.y = 165
    objectCross.isVisible = false
    sceneGroup:insert(objectCross)
    
    – code…
end

– code …[/lua]

Where I am wrong in here?

Thanks!

Regards.

I don’t really understand what your code is doing, but I can see you’re not following a good model for object creation and managment.
 
You’re much better off writing a factory function (builder) and using table listeners instead of a file level variable and standalone functions.
 
This example may have typos and may not do what you want, but focus on the ‘model’ I used to create and manage the object as well as its listeners:
 

local function sound() -- You showed this in your code, but didn't provide it. -- This is a placeholder for the example below. -- Your version must be defined BEFORE createBall() end local function createBall( group, x, y ) -- Tip: Double check the case of the file "objectimage.png" -- It it does not match this will fail on devices local ball -- display.newImageRect( group, "objectimage.png", 100, 80 ) ball.x = x ball.y = y function ball.collision( self, event ) if( event.phase == "began" ) then self:removeEventListener("collision") sound() local function onComplete() display.remove( self ) -- I NEVER use obj:removeSelf() end transition.to( self, { alpha = 0, time = 1000, onComplete = onComplete } ) end return false end ball:addEventListener("collision") return ball end function scene:create( event ) local sceneGroup = self.view local ball = createBall( sceneGroup, 275, 165 ) end

Again, may be totally missing the point of your prior code post… Hope that helped.