I'm asking for help for collisions! I can not understand how they work! I'll post my code that I hope help comes from some good

[lua]require “sprite”
local grabber = require(“SpriteGrabber”)
local movieclip = require “movieclip”

require ‘physics’
physics.start()
physics.setGravity(0,0)

local StickLib = require(“lib_analog_stick”)

– DISPLAY
_W = display.contentWidth
_H = display.contentHeight
local screenW = display.contentWidth
local screenH = display.contentHeight
local Text = display.newText( " ", screenW*.6, screenH-20, native.systemFont, 15 )

– HIDDEN STATUS BAR
display.setStatusBar(display.HiddenStatusBar)
–MULTITOUCH
system.activate( “multitouch” )
– Background
local baseline = display.contentHeight/2

local sky = display.newImage(“sky.jpg”)
local enemies = display.newGroup()

– BORDER

local borderDown = display.newRect(0, baseline + 140 ,480, 100)
borderDown:setFillColor(255,255,255,0)
physics.addBody( borderDown, ‘static’,{bounce = 1, friction = 0})

local borderUp = display.newRect(0, -60,480, 90)
borderUp:setFillColor(255,255,255,0)
physics.addBody( borderUp, ‘static’,{bounce = 1, friction = 0})

local borderLeft = display.newRect(0, 0, 20, _H)
borderLeft:setFillColor(255,255,255,0)
physics.addBody( borderLeft, ‘static’,{bounce = 1, friction = 0})

local borderRight = display.newRect(_W - 20, 0, 20, _H)
borderRight:setFillColor(255,255,255,0)
physics.addBody( borderRight, ‘static’,{bounce = 1, friction = 0})

– Scenes

local ufo = {}
ufo[1] = display.newImage(“mountain_big.png”)
ufo[1].xScale = 1.7; ufo[1].yScale = 0.7
ufo[1]:setReferencePoint(display.BottomCenterReferencePoint)
ufo[1].x = 50; ufo[1].y = baseline - 125
ufo[1].dx = 0.1

ufo[2] = display.newImage(“mountain_small.png”)
ufo[2].xScale = 0.6; ufo[2].yScale = 0.6
ufo[2]:setReferencePoint(display.BottomCenterReferencePoint)
ufo[2].x = 120; ufo[2].y = baseline + 100
ufo[2].dx = 0.2

ufo[3] = display.newImage(“Bamboo-rgba.png”)
ufo[3].xScale = 0.4; ufo[3].yScale = 0.4
ufo[3]:setReferencePoint(display.BottomCenterReferencePoint)
ufo[3].x = 480; ufo[3].y = baseline + 140
ufo[3].dx = 0.7

ufo[4] = display.newImage(“mountain_small.png”)
ufo[4].xScale = 0.7; ufo[4].yScale = 0.7
ufo[4]:setReferencePoint(display.BottomCenterReferencePoint)
ufo[4].x = 250; ufo[4].y = baseline + 100
ufo[4].dx = 0.4

ufo[5] = display.newImage(“Palm-arecaceae.png”)
ufo[5].xScale = 0.7; ufo[4].yScale = 0.7
ufo[5]:setReferencePoint(display.BottomCenterReferencePoint)
ufo[5].x = 180; ufo[4].y = baseline + 100
ufo[5].dx = 0.6

ufo[6] = display.newImage(“Greenhouse-Palm-jubaea01.png”)
ufo[6].xScale = 0.7; ufo[4].yScale = 0.7
ufo[6]:setReferencePoint(display.BottomCenterReferencePoint)
ufo[6].x = 290; ufo[4].y = baseline + 100
ufo[6].dx = 0.4

ufo[7] = display.newImage(“Greenhouse-Palm-jubaea01.png”)
ufo[7].xScale = 0.8; ufo[4].yScale = 0.7
ufo[7]:setReferencePoint(display.BottomCenterReferencePoint)
ufo[7].x = 210; ufo[4].y = baseline + 110
ufo[7].dx = 0.5

ufo[8] = display.newImage(“Bamboo-rgba.png”)
ufo[8].xScale = 0.4; ufo[8].yScale = 0.4
ufo[8]:setReferencePoint(display.BottomCenterReferencePoint)
ufo[8].x = 100; ufo[8].y = baseline + 150
ufo[8].dx = 0.7

– MOUNTAIN
local mountain_big = display.newImage(“mountain_big.png”, 132-150, 0)
local mountain_sma = display.newImage(“mountain_small.png”,-10, 161)
local mountain_big = display.newImage(“mountain_big.png”, 250, 40)
local mountain_sma = display.newImage(“mountain_small.png”, 300, 135)
local mountain_big = display.newImage(“mountain_big.png”,50,80)

– GRASS
local grass = display.newImage( “grass.png” )
grass:setReferencePoint( display.CenterLeftReferencePoint )
grass.x = 0
grass.y = baseline + 150
local grass2 = display.newImage( “grass.png” )
grass2:setReferencePoint( display.CenterLeftReferencePoint )
grass2.x = 480
grass2.y = 300

–GROUND
local ground = display.newRect( 0, baseline + 150, 480, 90 )
ground:setFillColor( 0x31, 0x5a, 0x18 )
local tPrevious = system.getTimer()
local function move(event)
local tDelta = event.time - tPrevious
tPrevious = event.time

local xOffset = ( 0.4 * tDelta )

grass.x = grass.x - xOffset
grass2.x = grass2.x - xOffset

if (grass.x + grass.stageWidth) < 0 then
grass:translate( 480 * 2, 0)
end
if (grass2.x + grass2.stageWidth) < 0 then
grass2:translate( 480 * 2, 0)
end

local i
for i = 1, #ufo, 1 do
ufo[i].x = ufo[i].x - ufo[i].dx * tDelta * 0.2
if (ufo[i].x + ufo[i].stageWidth) < 0 then
ufo[i]:translate( 480 + ufo[i].stageWidth * 2, 0 )
end
end
end
– Start everything moving
Runtime:addEventListener( “enterFrame”, move );
– PLAYER

local player = display.newImage(“goku1.png”)
player.name = ‘player’
player.x = screenW* .2
player.y = screenH* .5
physics.addBody(player,‘dynamic’,{bounce = 0})

– RECTANGLE FOR SHOOT

local rect = display.newRect(_W/2 + 93,_H/2,_W/2+ 150, _H/2)
rect:setFillColor(255,255,255, 30)
–Joystick/Player Movement

MyStick = StickLib.NewStick(
{
x = screenW*.1,
y = screenH*.85,
thumbSize = 16,
borderSize = 32,
snapBackSpeed = .75,
R = 255,
G = 255,
B = 255
} )

MyStick2 = StickLib.NewStick(
{
–x = screenW*.1,
–y = screenH*.85,

x = 550,
y = 850,

–Thumb size is inner circle, border size is outer circle

thumbSize = 40,
borderSize = 64,
snapBackSpeed = .75,
R = 255,
G = 0,
B = 0
} )

local function main( event )

– MOVE THE PLAYER
MyStick:move(player, 10.0, false)
MyStick2:move(player, 7.0, true)

– SHOW STICK INFO
Text.text = “ANGLE = “…MyStick:getAngle()…” DISTANCE = “…math.ceil(MyStick:getDistance())…” PERCENT = “…math.ceil(MyStick:getPercent()*100)…”%”

end

Runtime:addEventListener( “enterFrame”, main )
– SHOOT BULLET WITH TRANSITION

local onBullet = function( self, event )
bullet = display.newImage(‘bullet.png’)
bullet.x = player.x + bullet.contentWidth
bullet.y = player.y + 5
physics.addBody(bullet,{isSensore = true, isBullet = true})
transition.to(bullet, {x = player.x + 1200, y = player.y, time = 3000})
bullet.collision = onCollision; – If a collision is detected, fire the function onCollision.
bullet:addEventListenr( “collision”, bullet );
end

rect:addEventListener(‘tap’, onBullet)
– MY ENEMY SPRITE SHEET

local enemyTable = {}
local i = 0

local function foo(event)
if event.phase == “ended” then
print(event.target.name)
end
if event.target.name == “name is: 3” then
event.target:removeEventListener(“touch”, foo)
print(“you can add another listener here now”)
end
end

local function spawn_enemies()
i=i+1
enemyTable[i] = movieclip.newAnim{ “e1.png”, “e2.png”, “e3.png”, “e4.png”, “e5.png”, “e6.png” }
enemyTable[i].x = math.random(390,400)
enemyTable[i].y = math.random(1,350)
enemyTable[i]:play{ startFrame=1, endFrame=6, loop=0, remove=false }
physics.addBody(enemyTable[i],‘dynamic’, {isSensor= true, rotation = false, bounce = 0})
enemyTable[i]:setLinearVelocity(-100,0)
enemyTable[i].name = "name is: "…i
enemyTable[i]:addEventListener(“touch”, foo)

for i,v in pairs(enemyTable) do
print(i,v)
end
end

timer.performWithDelay(500, spawn_enemies, 5)
local onBulletCollision = function( self, event )
if event.phase == “began” and event.other.myName ==“bullet” then
enemyTable[i].isVisible = false

enemyTable[i]:removeSelf()
enemyTableSet:dispose()
levelGroup:remove(enemyTable[i])

Runtime:removeEventListener(“enterFrame”, movesprite)

end
end

[lua] [import]uid: 99231 topic_id: 21538 reply_id: 321538[/import]

Hi

I can’t see anything immediately wrong with your code, so not sure what to suggest.

Could you perhaps be a little more specific about exactly what the problem is? Are you getting error messages/objects passing through each other/a function not firing/collisions not registering etc?

One way I try to work around problems like this is to write a really basic, simpler program which isolates the problem you are having, and see if you can get that working. Sorry if that’s something you already do.

[import]uid: 67933 topic_id: 21538 reply_id: 85266[/import]

Sorry if I was not well explained! So my problem is that the bullet and the enemy, passing one above the other without any collision.
Also the bullet and the enemy will not automatically remove! help me!

I read on the forum of CORONA, that it is recommended to set the physical properties of (isSensor = true) to be the best collision … help! [import]uid: 99231 topic_id: 21538 reply_id: 85505[/import]

Ok, let’s look at the collision first and once that is working, you can tackle the automatic removal.

The “isSensor = true” will still register that a collision has taken place, but will not effect the objects movement/velocity etc.

if isSensor = true, objects pass through each other, (but Corona still registers the collision)

if isSensor = false, the objects will collide and bounce off each other.

For starters, try setting up a really basic program, where you fire a bullet object at a player object.

sometimes collisions will fail to register because the bullet is moving faster than the collision checks your program is doing.

try setting the bullet objects to object:isBullet = true. That may help.

Another possible issue is that the player and bullet objects are in different groups. Collisions wont register between objects in different groups.

Again, I think the best thing to do is write a simple program to get collisions working, then once you’ve got it figured out or isolated the problem, it’ll be easier to identify a suitable solution. I’m sorry, but I just don’t have time to read a large program, especially if it’s not plug and play code.

Hope the suggestions help.

Spider. [import]uid: 67933 topic_id: 21538 reply_id: 85569[/import]

I place to you the link where you can find my game, please help me … I would like to understand how these damn collisions happen!

http://hotfile.com/dl/146399168/b6a1bcc/My_Game.zip.html [import]uid: 99231 topic_id: 21538 reply_id: 86597[/import]

Hey.

That’s not looking too bad so far.

I’ve taken a very quick look at your code. The first thing you need to do is stop the errors printing to the simulator. find every instance of *.stagewidth and replace it with *.contentWidth

Your program will function exactly the same, but you’ll be able to see what’s going on.

Having done that, you’ll see there’s an error in your bullet collision listener on line 229. I haven’t had time to work out what the problem is yet as have to go to work now.

Try and resolve that and get a collision to print to the simulator whenever it is detected.

Also, in your question, you still don’t post what you’re having trouble with? bullets hitting trees/enemies. Player character hitting enemies? What are you trying to acheive?

Thanks

Spider [import]uid: 67933 topic_id: 21538 reply_id: 86601[/import]

the problem is that the bullet passes through the enemy without hitting him.
Also the enemy does not remove self! thanks [import]uid: 99231 topic_id: 21538 reply_id: 86605[/import]

I am maybe blind, but i cant see that you are referencing the right function for the collision?

Joakim [import]uid: 81188 topic_id: 21538 reply_id: 86626[/import]

Hmm, I was on the phone before and now I am looking at the code on my computer.
Wouldn’t bullet:addEventListenr( “collision”, bullet ) be

bullet:addEventListenr( “collision”, onBulletCollision )???

Joakim
EDIT: It is also misspelled in the code, bullet:addEventListenr should be bullet:addEventListener

[import]uid: 81188 topic_id: 21538 reply_id: 86628[/import]

hello, thanks for your warning, but I had already noticed this error, but changing this does not happen the collision! I’m desperate, I’m trying to understand these damn collisions for a month! HELP! [import]uid: 99231 topic_id: 21538 reply_id: 86834[/import]

Valerio, I don’t want to appear mean or anything, but it took me no time to clear the simulator of *.stagewidth errors. Once they were gone, the simulator clearly indicated that the error is on line 229. jkrassman then kindly wrote out the correct code and corrected syntax errors.

If it still isn’t working, you need to post what error messages you are getting? what code are you working with now? Has anything changed since the last code? Have you got it to print “works” to the simulator when a collision is detected, (so you can actually monitor if the event is firing?)

There are loads of people who will be happy to help you on the forums, but you must be more precise. We can’t correct the problem if we don’t know what error messages you get and what code you are using.

Thanks

Spider. [import]uid: 67933 topic_id: 21538 reply_id: 86835[/import]

excuse me … Yesterday, on your advice, I changed stageWidth with contentWidth and the simulator has stopped giving errors.

The cool thing about it is that gives me no error messages! For this reason, I can not seem to be accurate.
my problem is the collision between enemy / projectile, with the removal from the display of both …
this does not happen even if the simulator does not report errors, the two objects you pass one over the other without touching. [import]uid: 99231 topic_id: 21538 reply_id: 86837[/import]

Remove all code in the function for collision and ad a print statement:

print(“Collision”);

If that doesn’t work, theres something else…

Joakim [import]uid: 81188 topic_id: 21538 reply_id: 86839[/import]