Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

How to add Score with collission
Started by Gallicchiojoe907 Jun 20 2019 12:55 AM

No replies to this topic
collission help game
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

Gallicchiojoe907

[GLOBAL: userInfoPane.html]
Gallicchiojoe907
  • Observer

  • 1 posts
  • Corona SDK

Hey guys, below is my code. My aim is: whenever mario touches the coin the score rises. If you know how to help, please reply thanks!

 

local physics = require( "physics" )
physics.start()
 
 
 
local score = display.newText(0, display.contentCenterX,50)
score.size = 75
 
local sprite = display.newImage("mario.png")
sprite.x = display.contentCenterX
sprite.y = display.contentCenterY
sprite:scale(0.4,0.4)
physics.addBody( sprite, "static", { density=3.0, friction=0.5, bounce=0.3 } )
sprite.myName = "Mario"
 
 
local left = display.newImage("left.png", 0,250)
left:scale(0.05,0.05)
 
local right = display.newImage("left.png", 500,250)
right:rotate(180)
right:scale(0.05,0.05)
 
 
local up = display.newImage("left.png", 500,100)
up:scale(0.05,0.05)
up:rotate(90)
 
local down = display.newImage("left.png", 0, 100)
down:scale(0.05,0.05)
down:rotate(-90)
 
 
 
local coin = display.newImage("coin.png")
coin.x = math.random(500)
coin.y = math.random(350)
coin:scale(0.03,0.03)
physics.addBody( coin, "static", { density=3.0, friction=0.5, bounce=0.3 } )
coin.myName = "coin1"
 
local coin2 = display.newImage("coin.png")
coin2.x = math.random(500)
coin2.y = math.random(350)
coin2:scale(0.03,0.03)
physics.addBody( coin2, "static", { density=3.0, friction=0.5, bounce=0.3 } )
coin.myName = "coin2"
 
local coin3 = display.newImage("coin.png")
coin3.x = math.random(500)
coin3.y = math.random(250)
coin3:scale(0.03,0.03)
physics.addBody( coin3, "static", { density=3.0, friction=0.5, bounce=0.3 } )
coin.myName = "coin3"
 
local coin4 = display.newImage("coin.png")
coin4.x = math.random(500)
coin4.y = math.random(250)
coin4:scale(0.03,0.03)
physics.addBody( coin4, "static", { density=3.0, friction=0.5, bounce=0.3 } )
coin.myName = "coin4"
 
local coin5 = display.newImage("coin.png")
coin5.x = math.random(500)
coin5.y = math.random(350)
coin5:scale(0.03,0.03)
physics.addBody( coin5, "static", { density=3.0, friction=0.5, bounce=0.3 } )
coin.myName = "coin5"
 
 
 
local function moveleft()
  transition.to(sprite, {x=sprite.x-20, time=100})
end
 
local function moveright()
  transition.to(sprite, {x=sprite.x+20, time=100})
end
 
local function moveup()
  transition.to(sprite, {y=sprite.y-20, time=100})
end
 
local function movedown()
  transition.to(sprite, {y=sprite.y+20, time=100})
end
 
local function add()
  score.text = score.text+1
 
end
 
local function subtract()
  score.text = score.text-1
end
 
 
local function onGlobalCollision( event )
  if (event.phase == "began") then
    if sprite.myName == coin.myName then
      add()
    end
  end
end
 
Runtime:addEventListener( "collision", onGlobalCollision )
 
 
 
 
left:addEventListener("tap", moveleft)
right:addEventListener("tap", moveright)
up:addEventListener("tap",moveup)
down:addEventListener("tap", movedown)
 



[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: collission, help, game