Force removal and newText placement question

In my code I have a couple rats that when they get hit by an object, they change frame and get a score based on how hard they are hit. The harder hit removes them, frame three is a puff of smoke.

Did I do this correct? It does work.

My other question is the commented out destructionScore. I would like the score received to pop up angry birds like over the rat. But since I have multiple rats in different locations, I can’t figure out how to retrieve their x, y locations with out creating a different onRatCollision function for each rat. I don’t want to do that because I do have a bunch of other objects that I will be doing this type of onCollision to and it could end up being a lot of code.

Thanks

Dan

[code]

local Rathita = 0

local function onRatCollision ( self, event )

if Rathita == 0 then
if ( event.force > 30.0 ) then
self:stopAtFrame(2)
Rathita = 1
score = score + 15
t.text=score
end
end
if Rathita == 1 then
if ( event.force > 45.0 ) then
self:stopAtFrame(3)
self:removeEventListener( “postCollision”, self )
local function removeRat( event )
self:removeSelf()
end
timer.performWithDelay( 200, removeRat)
score = score + 150
t.text=score

– local destructionScore = display.newText(score, x, y, native.systemFont, 10 )
– destructionScore:setTextColor( 255, 0, 0)
– game:insert( destructionScore )

end
end

end

Rat1.postCollision = onRatCollision
Rat1:addEventListener( “postCollision”, Rat1 )

Rat2.postCollision = onRatCollision
Rat2:addEventListener( “postCollision”, Rat2 )
[/code] [import]uid: 78446 topic_id: 14080 reply_id: 314080[/import]

Heya :slight_smile:

Try event.x and event.y for your x and y values.

If you have any issues with that, or errors, bump up the commented out section (uncommented, of course) to just below where you updated the score.

That should do it :slight_smile:

Peach [import]uid: 52491 topic_id: 14080 reply_id: 51863[/import]

Thanks Peach,
I had to change it a little, the above code it would show the wrong number from what I meant.

[code]
local function onRatCollision ( self, event )
if Rathita == 0 then
if ( event.force > 45.0 ) then
self:stopAtFrame(2)
Rathita = 1
score = score + 15
t.text=score
end
end
if Rathita == 1 then
if ( event.force > 35.0 ) then
self:stopAtFrame(3)
self:removeEventListener( “postCollision”, self )
points=150
score = score + points
t.text=score

destructionScore = display.newText(“points”, event.x, event.y, native.systemFont, 40 )
destructionScore:setTextColor( 255, 0, 0)

local function removeRat( event )
self:removeSelf()
end

timer.performWithDelay( 700, removeRat)
end
end

end[/code]

The above code place the 150 in the top left corner of the screen and stays there.

Now if I try below, the 150 doesn’t display at all.

 destructionScore = display.newText("", event.x, event.y, native.systemFont, 40 )  
 destructionScore:setTextColor( 255, 0, 0)   
 destructionScore = points  

in the end I will actually want the 150 number to appear and disappear with my frame 3 like below.

  
 local function removeRat( event )   
 destructionScore = points  
 self:removeSelf()   
 end  

Thanks

Dan [import]uid: 78446 topic_id: 14080 reply_id: 51931[/import]

OK, so, can you access the X and Y of the collision when it happens? The rat’s x and y?

rat.x and rat.y, maybe? [import]uid: 52491 topic_id: 14080 reply_id: 51970[/import]

I named the rats as Rat1.myName = “rat”, Rat2.myName = “rat” and so on.

if I use

  
  
 destructionScore = display.newText(points, rat.x, rat.y, native.systemFont, 40 )  
 destructionScore:setTextColor( 255, 0, 0)   
  
  

I get a runtime error, attempt to index global ‘rat’ < a nil value>

now if I use this-

  
  
 destructionScore = display.newText(points, Rat1.x, Rat1.y, native.systemFont, 40 )  
 destructionScore:setTextColor( 255, 0, 0)   
  

No error, but no text on the screen either

Dan [import]uid: 78446 topic_id: 14080 reply_id: 52014[/import]

Yeah, that wont work if the rats have numbers - because you’ll say “rat1” but it might be “rat2” or the like. (The name thing wont help either in this scenario.)

If you put print (event.x) at line 4 or 5 in the above example you posted, what prints? A number value or nil? [import]uid: 52491 topic_id: 14080 reply_id: 52075[/import]

(Please post your code in < lua > or < code > tags so I can read it.)

It’s printing it over and over as soon as the level starts? That isn’t right :S

Are you constantly killing rats? It should only print on collision.

Are the rats colliding with something other than whatever you are throwing at them? I don’t see how the event is being triggered over and over again once you start the app from what I see above in your code. [import]uid: 52491 topic_id: 14080 reply_id: 52267[/import]

In here it prints the X-1 Y-1 over and over as soon the level starts. In the Rathita it only comes up X-1 Y-1 once, when it occurs.

 local function onRatCollision ( self, event )  
 print( "Rat Kill Event X-"..event.x.."Y-"..event.y )  
 if Rathita == 0 then  
 if ( event.force \> 45.0 ) then  
 self:stopAtFrame(2)  
 Rathita = 1  
 score = score + 15  
 t.text=score   
 end  
 end  
 if Rathita == 1 then  
 if ( event.force \> 35.0 ) then  
 self:stopAtFrame(3)  
 points=150  
 score = score + points  
 t.text=score   
 kills = kills + 1   
 print( "kills:"..kills )   
 --destructionScore = display.newText(points, event.x, event.y, native.systemFont, 40 )  
 --destructionScore:setTextColor( 255, 0, 0)   
 --game:insert( destructionScore )  
 local function removeRat( event )   
print( "Rat Kills in remove -"..kills )   
print( "points in remove -"..points )   
 self:removeSelf()   
 -- destructionScore=""  
 end   
 self:removeEventListener( "postCollision", self )   
 timer.performWithDelay( 600, removeRat)   
 end  
 end   
 end   
  
 -- Set table listeners in each rat to check for collisions  
 Rat1.postCollision = onRatCollision  
 Rat1:addEventListener( "postCollision", Rat1 )  
  
 Rat2.postCollision = onRatCollision  
 Rat2:addEventListener( "postCollision", Rat2 )  

sorry about the code, I thought I did that [import]uid: 78446 topic_id: 14080 reply_id: 52214[/import]

There is a collision going on. The rats are on other physics bodies stacked up. I am going to add collisions to them like the rats, but not until the rats are working. The only collision is when the level starts and it all settles into place. [import]uid: 78446 topic_id: 14080 reply_id: 52422[/import]

So you’re saying it prints “over and over as soon the level starts” but that the collision only happens once and it all settles into place?

Either it keeps printing (because there ARE collisions going on) or it stops printing (because the collisions are over).

Maybe you should put the collision listener on whatever you are throwing at the rats instead? (Or explain which of the above is happening ;)) [import]uid: 52491 topic_id: 14080 reply_id: 52456[/import]

Hey,

OK - wow. Firstly, your config.lua file is messed up. That error that prints every time you load isn’t a good thing; but here’s a fixed version;

[lua]application =
{
content =
{
width = 320,
height = 480,

scale = “letterbox”,

imageSuffix =
{
["@2x"] = 2
}
}
}[/lua]

Next - your images aren’t setup well. Some start with caps, others do not. Then you call some with caps that don’t have caps and some without caps that need caps. It was error central and your level wouldn’t even load on .591 until it was fixed. You need to look at that.

For the level itself - I can’t hit the rats. The ball drops, hits the pipe, bounces back, hits the board thing again, the flies off to the right over the rats and disappears.

There seems to be no way to get back to the first half of the screen to launch another ball, either.

So, while I’d like to help you I can’t do it if I can’t replicate the problem - and I can’t.

If you can give me a file showing the exact problem I will try to help you but with that project I cannot make the ball hit the rats - it just flies off extremely fast.

Let me know - I’m sorry if the above sounds harsh - it’s just errors make it very difficult to work with code.

Peach [import]uid: 52491 topic_id: 14080 reply_id: 52632[/import]

Harsh is no problem, I’ve looked at it so long I miss stuff stuff, fresh eyes really help.

Your problem of not getting a new ball is the problem.

If you still have it, drop the ball so it just hits the ground, for me it resets once, I can drop a ball a second time, but not again, it doesn’t reset or end, I’m wondering if I have it designed wrong? I may have to use a button so another new ball can drop, but I was trying to get it to reset or end automatically.

I don’t know which part of the code is the problem. The oncollisions or the function newround?

Thanks for your help alot.

Dan [import]uid: 78446 topic_id: 14080 reply_id: 52659[/import]

I am sometimes getting a second ball - what I wanted to help you with was getting the X and Y of your rat collisions - however until you sort out a few other issues, I can’t do that. (When you have I’m happy to try and make time to take another look.)

I’m glad you didn’t take my words badly; I do think the game looks good, your graphics are nice, the claw handles beautifully - but obviously there is some work to be done.

As one suggestion - keep your images named the same way through out. Give them all caps or none caps. You will be less likely to make mistakes that way :wink:

Peach [import]uid: 52491 topic_id: 14080 reply_id: 52724[/import]

Wow was right, once the config was fixed it was crazy, I’ll have to go back to my small images, I wondered what had happened. I’ll get those and named correct.

While I do that, could two function events going at the same time cause the problem? One with the orb and one with the rats?

Thanks
Dan [import]uid: 78446 topic_id: 14080 reply_id: 52793[/import]

What is each function doing? Are they trying to manipulate the same object or anything like that?

If they’re not, it shouldn’t be an issue. [import]uid: 52491 topic_id: 14080 reply_id: 52856[/import]

It’s printing, there is a collision going on, but I don’t see it.

[import]uid: 78446 topic_id: 14080 reply_id: 52510[/import]

The two funtions aren’t calling the same thing, the one is the orb hitting objects including the rats and the other is the rats being hit through force.

Well I fixed it up
http://phryz.com/JunkYardDog.zip

In collisions with the objects, it resets twice just fine and ends the game correctly if you get 2 kills or run out of orbs( it just goes back to the menu screen for now).

The rats are still showing constant force. I lowered their density and it just lowers the force amount. The event.x,event.y are still showing up at x-1 & y-1. I even removed everything but 1 rat and it still always showed a force.

 local function onRatCollision( self, event )  
  
 print( "rat Force: " .. event.force )  
  
 if event.force \> .7 then  
 print( "after force x-" .. event.x.."y-".. event.y )  
 print( "rat destroyed! Force: " .. event.force )  
 kills = kills + 1  
 points=150  
 score = score + points  
 t.text=score   
 self:stopAtFrame(3)  
 local destructionScore = display.newText("", event.x,event.y, native.systemFont, 15 )  
 destructionScore:setTextColor( 255, 0, 0)   
 game:insert( destructionScore )  
 destructionScore.text=points  
 print( "Kills: " .. kills )  
  
 local function removerat( event )   
 self:removeSelf()   
 self = nil  
 end  
 transition.to( self, { time=200, alpha=1.0, onComplete=removerat } )  
  
 end  
 end  
  
 rat1.postCollision = onRatCollision  
 rat1:addEventListener( "postCollision", rat1 )  
  
  
 rat2.postCollision = onRatCollision  
 rat2:addEventListener( "postCollision", rat2 )  

I put in a lot of prints. I am noticing that I am getting multiple prints of the same thing. Could something that is printing those multiple times also be reading the force over and over?

Thanks,
Dan [import]uid: 78446 topic_id: 14080 reply_id: 53314[/import]

Wait, why are we looking at force? Or just because?

I thought we were trying to grab the event.x and event.y for your text? (Sorry if I’ve missed something!)

For them being triggered more than once, do either;
[lua]if event.phase == “began” then[/lua]
or
[lua]if event.phase == “ended” then[/lua]

That should sort it out.
… ARGH! I have just downloaded your project and am still getting a errors when I start level 1.

*BowlingBall/bowlingBall error
*backbutton error
Also, I can get the coords for your text now, if you want them; put this in your rat collision function;
[lua]print (event.target.x, event.target.y)[/lua]

That tells you the x and y.

So rather than printing you could use that to spawn your text/points.

That help? :slight_smile: [import]uid: 52491 topic_id: 14080 reply_id: 53340[/import]

Thanks that was what I was looking for!

Now I’m going to clean it up and add similiar collisions to the other objects.

Dan

[import]uid: 78446 topic_id: 14080 reply_id: 53488[/import]

Whew, excellent :slight_smile: [import]uid: 52491 topic_id: 14080 reply_id: 53522[/import]