my object's height does not grows correctly

Hi All,

My code is below.

I want growing my ground object when my ball objects move.

After a while the ball is not grow towards…

What could be the error?

thank you now…

My Code:

[lua]_W = display.contentWidth;
_H = display.contentHeight;
motiony = 0;
speed = 2;

local physics = require( “physics” )
physics.start()

local ball = display.newCircle( 50, 50, 20 )
ball:setReferencePoint(display.TopCenterReferencePoint)
ball.x = _W/2; ball.y = 50;

local ground = display.newRect( 0, 0, 20, 65 )
ground:setReferencePoint(display.TopCenterReferencePoint)
ground.x = _W/2; ground.y = 0
physics.addBody( ground, “static”, { friction=0.5, bounce=0.3 } )

local box = display.newRect( 0, 0, 60, 80 )
box:setReferencePoint(display.TopCenterReferencePoint)
box.x = 160; box.y = 440
box:setFillColor( 255, 0, 0 )

local function stop (event)
if event.phase ==“ended” then
motiony = 0;
end
end
Runtime:addEventListener(“touch”, stop )

local function moveball (event)
ball.y = ball.y + motiony;
ground.height= ball.y + ball.height --ground.height + motiony
end
Runtime:addEventListener(“enterFrame”, moveball)

function box:touch()
motiony = speed;
end
box:addEventListener(“touch”,box)
[/lua]

Hey,

Ok, so from what I am reading above… when you touch the ball you want the ground object to grow? When is it that you want your stop() listener function to run? When you touch the box it will increase motionY by your value of speed, hence growing your ground object - but then touching the screen will also invoke your stop function which will reset the value of motionY to 0, which might be what is causing the ground object not to grow.

Probably best to stick some print() statements in your moveball and box’s touch functions to see what is happening to the values of ball.y, ground.height and motionY over time.  Do this, run your code and post your output.

Regards,

Rich

Hi Richi,

i am printing my values like below:

local function moveball (event)
ball.y = ball.y + motiony;
ground.height= ball.y + ball.height
print(“ground.height:” … ground.height)
print(“ball.y:” … ball.y)
print(“ball.height :” … ball.height)
end

i am seeing;

ground.height = 196

ball.y = 156

ball.height = 40

but my ball does not touch my ground  :( 

Ah, you just need to add ball to the physics engine, as you did with ground. Make it “dynamic” though rather than “static” - then your ball should sit on top of your ground

Regards
Rich

Hi richi,

I tried but it did not change anything  :frowning:

Hey,

To be honest, I am still not 100% clear what it is that you want to happen :stuck_out_tongue: In your original post you said that the ground object stops growing after a certain time - and more recently you have said that the ball is not touching the ground object.  Tell me what it is that you want to happen.

Regards,

Rich

Hi richi,

can you try to run my code in corona simulator?

you will see After a while the ball does not tocuh the ground. between ball and ground you will see a space  :) 

i want make ground top position never change but bottom position should equel to ball top position.

so my ground should grown to ball position  :) 

I can’t mate - i’m at work at the moment! The only reason I am on here at the moment is because we’re still within a Production code-freeze period :slight_smile: If you generally want the ball to always be on top of the ground and move up/down depending on what value the ground.height is set to, then you need to use physics engine and possible omit the setting of ball.y altogether

I can certainly have a look later on - but hopefully somebody else might be able to intercept and tell you exactly what is going on :slight_smile:

Regards,

Rich

Hi richie,

I hope you (or someone else) will look and tell me any solve. 

i am printing my values. values are correct but code not working properly.

really i didnt understand  :slight_smile:

First of all, you don’t need the semi-colons after the assignment statements - this is Lua, not C++ or Java.   :slight_smile:

Second, the code is doing exactly what it is supposed to be doing, namely moving the ball as long as you are touching the red box.

Why are you adding physics properties only to the ground?  Why is the ground a small vertical rectangle at the top of the screen?

I’m really not sure what you are trying to accomplish here - have you checked out the tutorials and great sample code repository that comes with Corona?

Hi again all (richie, dave…),

I need to tell you my problem one more time.

How about the “ground”? There is space between the ball and the ground. How can i fix it?

Please see the atached screen shots.

Please understand my issue and help meeeee :)  :)  :slight_smile:

What are you trying to do?   Do you want the ground to move with the ball and grow in height, or just move with the ball?   Do you want  the ball to always be at the end of the ground?

I got it guys, thanks all…

Dave, i have wanted  ground to move with the ball and grow in height and i did…

There was a missing part on my code…

Missing part is:

local function moveball (event)

ball.y = ball.y + motiony;

ground:setReferencePoint(display.TopCenterReferencePoint)  (Missing part 1)

ground.y=0  (Missing part 2)

ground.height= ball.y + (ball.height/2) --ground.height + motiony

end

and my ground was growning both side…  :)  :slight_smile:

Hey,

Ok, so from what I am reading above… when you touch the ball you want the ground object to grow? When is it that you want your stop() listener function to run? When you touch the box it will increase motionY by your value of speed, hence growing your ground object - but then touching the screen will also invoke your stop function which will reset the value of motionY to 0, which might be what is causing the ground object not to grow.

Probably best to stick some print() statements in your moveball and box’s touch functions to see what is happening to the values of ball.y, ground.height and motionY over time.  Do this, run your code and post your output.

Regards,

Rich

Hi Richi,

i am printing my values like below:

local function moveball (event)
ball.y = ball.y + motiony;
ground.height= ball.y + ball.height
print(“ground.height:” … ground.height)
print(“ball.y:” … ball.y)
print(“ball.height :” … ball.height)
end

i am seeing;

ground.height = 196

ball.y = 156

ball.height = 40

but my ball does not touch my ground  :( 

Ah, you just need to add ball to the physics engine, as you did with ground. Make it “dynamic” though rather than “static” - then your ball should sit on top of your ground

Regards
Rich

Hi richi,

I tried but it did not change anything  :frowning:

Hey,

To be honest, I am still not 100% clear what it is that you want to happen :stuck_out_tongue: In your original post you said that the ground object stops growing after a certain time - and more recently you have said that the ball is not touching the ground object.  Tell me what it is that you want to happen.

Regards,

Rich

Hi richi,

can you try to run my code in corona simulator?

you will see After a while the ball does not tocuh the ground. between ball and ground you will see a space  :) 

i want make ground top position never change but bottom position should equel to ball top position.

so my ground should grown to ball position  :) 

I can’t mate - i’m at work at the moment! The only reason I am on here at the moment is because we’re still within a Production code-freeze period :slight_smile: If you generally want the ball to always be on top of the ground and move up/down depending on what value the ground.height is set to, then you need to use physics engine and possible omit the setting of ball.y altogether

I can certainly have a look later on - but hopefully somebody else might be able to intercept and tell you exactly what is going on :slight_smile:

Regards,

Rich