this is my code .. I'm not very handy .. I'm trying to learn! I can not seem to occur in the collision between the enemy and the

require "sprite"
require 'physics'
physics.start()
physics.setGravity(0,0)
local grabber = require("SpriteGrabber")

local StickLib = require("lib_analog_stick")

local update = {}

-- 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})

-- Scene

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

-- MONTAGNE
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* .5
player.y = screenH* .5
physics.addBody(player,'kynematic',{bounce = 0})

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 SHIP
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 )

local bullet = function( self, event )
bullet = display.newImage('bullet.png')
bullet.x = player.x + 60
bullet.y = player.y

transition.to(bullet, {x = player.x + 1200, y = player.y, time = 3000})
bullets.insert(bullets, bullet)
end

rect:addEventListener('tap', bullet)

local enemy = display.newImage('enemy.png')
enemy.y = math.floor(math.random() * (display.contentWidth/2 - enemy.width))
enemy.x = display.contentHeight - 20
enemy.name = 'enemy'
physics.addBody(enemy)
enemy.bodyType = 'static'
enemies.insert(enemies, enemy)
enemy:addEventListener('collision', onCollision)

function onCollision( self, event )
if ( event.phase == "began" ) then
if( event.other.objectName =="enemy" ) then
event.target:removeSelf()
event.other:removeSelf()
bullet:removeEventListener( "collision", bullet )
end

end
end

Hey there,

Normally you'd give the thread a title, explain your issue then post your code between lua tags, like so;

< lua > code goes here < / lua >

Without the spaces, of course.

This is a little long to read over thoroughly and can't be tested as it isn't plug and play, however Corona For Newbies - Part 4 covers collision detection - link; http://techority.com/2011/06/19/corona-for-newbies-part-4-physics/

Please check that out and let me know how you get on.

Peach :)

views:2575 update:2012/2/9 11:37:26
corona forums © 2003-2011