[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]

## brick break's game : question Started by yvandotet Jun 10 2018 09:47 AM

6 replies to this topic

Best Answer yvandotet , 10 June 2018 - 01:25 PM

Ok I finally find what was erroneous in the code.

I also thinking that the docs speaks about changing the value of ball.x during the collision, but reading the value is allowed and does no disturbances.

In fact during my calculation I done one moment a wrong calculation that replace vx by an invalid number, something like √(-230) !

Thanks and Ciao

[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

### yvandotet

[GLOBAL: userInfoPane.html]
yvandotet
• Contributor

• 174 posts
• Corona SDK

hello

i was talking about a bug involve during the collision between the ball and the horizontal bar, and i found that it was because I used ball.x in the collision process.

But I have one question that I was unabled to solve :

when the ball arrives to the horizontal bar from the right side, and touch the left part of the bar , the ball simply bounce and continue to the left side.

But when the ball touch the right part of the bar, we want that the ball go back to the right side.

And at that point, I am block, because I need to calculate the difference between ball.x and bar.x, to check if it's positive or negative.

```function ball:collision(event)
local ph=event.phase
local x,y=event.x,event.y
local other=event.other

if ph=="began" then
if other.myName=="bloc" then other.delete() end // the brick to break

elseif ph=="ended" then
local vx,vy=self:getLinearVelocity()

if other.myName=="bar" then
local dx = self.x-bar.x // here is the problem : I need to calculate this, but this create bug !
if dx*vx<0 then vx=-vx end // changing direction

self:setLinearVelocity( vx,vy )
end
end

end

thanks you

[TOPIC: post.html]
#2

### ldurniat

[GLOBAL: userInfoPane.html]
ldurniat
• Contributor

• 355 posts
• Corona SDK

According to documentation (see Important table) you can't call object.x method during collision event.

Try (not tested)

```timer.performWithDelay( 10, function()    if other.myName=="bar" then
local dx = self.x-bar.x // here is the problem : I need to calculate this, but this create bug !
if dx*vx<0 then vx=-vx end // changing direction

self:setLinearVelocity( vx,vy )
end
end )```

ldurniat

[TOPIC: post.html]
#3

### roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
• Corona Geek

• 6,977 posts
• Corona SDK

Regardless of what the docs say I don't buy that.  You should be able to READ the  x and y properties.

Writing these properties would be an issue, but reading is fine.

Nomenclature Tip: The docs incorrectly use the term 'call' and then include the properties x and y in the list.

Calling is something done to a function or method.

Properties (fields on an object) are something you read and write.

Lastly, most properties can be read at any time as long as the object is valid and the properties still exist.

Edited by roaminggamer, 10 June 2018 - 01:26 PM.

• ldurniat likes this

[TOPIC: post.html]
#4

### roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
• Corona Geek

• 6,977 posts
• Corona SDK

```local wall = display.newRect( cx + 138, cy - 150,  310, 40 )
wall.rotation = 45
local wall = display.newRect( cx - 100, cy + 150,  300, 40 )
wall.rotation = 45
local wall = display.newRect( cx + 134, cy + 100,  400, 40 )
wall.rotation = 135
local wall = display.newRect( cx - 102, cy - 100,  400, 40 )
wall.rotation = 135

local ball = display.newCircle( cx, cy - 50, 20)
ball:setLinearVelocity( 4000, -2000)

local label = display.newText( "", left + 150, top + 50 )

function ball.collision( self, event )
label.text = "< " .. tostring( math.floor( self.x ) ) .. ", " .. tostring( math.floor( self.y ) ) .. " >"
-
```

[TOPIC: post.html]
#5

### roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
• Corona Geek

• 6,977 posts
• Corona SDK

Back to the original post:

``` local dx = self.x-bar.x // here is the problem : I need to calculate this, but this create bug !
```

The only reason that will created a bug is if self or bar is an invalid object and has been deleted.

Check your cleanup code to ensure that the objects are being correctly disposed of.

My guess is if you do this,

```print( "bar", bar.setLinearVelocity, type(bar.setLinearVelocity) == "function" )
print( "self", self.setLinearVelocity, type(self.setLinearVelocity) == "function" )
local dx = self.x-bar.x // here is the problem : I need to calculate this, but this create bug !
```

, when your game crashes, you'll see this printed before the crash:

```bar, nil, false
```

or (less likely)

```self, nil, false
```

[TOPIC: post.html]
#6

### roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
• Corona Geek

• 6,977 posts
• Corona SDK

Lastly, I'm not sure why you are referencing 'bar'.  'other' is the thing you hit.

This seems more correct:

```function ball:collision(event)
local ph=event.phase
local x,y=event.x,event.y
local other=event.other

if ph=="began" then
if other.myName=="bloc" then other.delete() end // the brick to break

elseif ph=="ended" then
local vx,vy=self:getLinearVelocity()

if other.myName=="bar" then
local dx = self.x-other.x
if dx*vx<0 then vx=-vx end // changing direction

self:setLinearVelocity( vx,vy )
end
end

end
```

[TOPIC: post.html]
#7

### yvandotet

[GLOBAL: userInfoPane.html]
yvandotet
• Contributor

• 174 posts
• Corona SDK

Ok I finally find what was erroneous in the code.

I also thinking that the docs speaks about changing the value of ball.x during the collision, but reading the value is allowed and does no disturbances.

In fact during my calculation I done one moment a wrong calculation that replace vx by an invalid number, something like √(-230) !

Thanks and Ciao

[topic_controls]

[/topic_controls]