0
votes

I'm fairly new to the Corona SDK and LUA.I hope someone can help me. I'm stuck now for quite a while on my array and evaluating, based on the tapped object, in a function what object has been passed to the function.

What I try to accomplish? Based on an Array I spawn a car (image), when the user taps this car the car is removed from the screen and the score is updated.

What I would like to accomplish is to set a different score value (100) for the blue car (bluecar.png) and when all other cars are tapped a score of 5.

function createCar()
  carArray = { "carblue.png", "cargreen.png", "carred.png", "caryellow.png"  }
  car = display.newImage( carArray [ math.random (#carArray ) ] )
  car.x = -200
  car.y = math.random (0, display.contentHeight)
  car.xScale = 0.2
  car.yScale = 0.2
  car:rotate (90)

  car.moves = transition.to (car, {time = 3000,  x=display.contentWidth-30 , y=math.random(0,display.contentHeight), onComplete = hitWall } )
  car:addEventListener ( "tap", touchCar )

  return car

end

function touchCar(event)
  local cartouched = event.target
  transition.cancel ( event.target.moves )
  audio.play(brake)
  cartouched:rotate (370)

  if  cartouched == carArray[1] then
    score = score + 100
      else score = score +5
  end
    scorelabel.text = "Score: ".. score 
    local function removeCar()
        display.remove(cartouched)
    end 
  timer.performWithDelay(600, removeCar)

createCar()
end

So how can I evaluate what car has been touched so for example when the blue car has been tapped the code adds 100 to the score, else 5.

Any help is very much appreciated.

2

2 Answers

0
votes
local BLUE_CAR = 1    --<------

function createCar()
  carArray = { "carblue.png", "cargreen.png", "carred.png", "caryellow.png"  }
  carType = math.random (#carArray )    --<------
  car = display.newImage( carArray [ carType ] )    --<------
  car.x = -200
  car.y = math.random (0, display.contentHeight)
  car.xScale = 0.2
  car.yScale = 0.2
  car:rotate (90)

  car.type = carType    --<------

  car.moves = transition.to (car, {time = 3000,  x=display.contentWidth-30 , y=math.random(0,display.contentHeight), onComplete = hitWall } )
  car:addEventListener ( "tap", touchCar )

  return car

end

function touchCar(event)
  local cartouched = event.target
  transition.cancel ( event.target.moves )
  audio.play(brake)
  cartouched:rotate (370)

  if  cartouched.type == BLUE_CAR then    --<------
    score = score + 100
      else score = score +5
  end
    scorelabel.text = "Score: ".. score 
    local function removeCar()
        display.remove(cartouched)
    end 
  timer.performWithDelay(600, removeCar)

createCar()
end
0
votes

You can give id to every car and evalute the id, if id of blue car is encountered update the score. following code may help you :

   function createCar()
   carArray = { "carblue.png", "cargreen.png", "carred.png", "caryellow.png"  }
   car = display.newImage( carArray [ math.random (#carArray ) ] )
   car.x = -200
   car.y = math.random (0, display.contentHeight)
   car.xScale = 0.2
   car.yScale = 0.2
   car:rotate (90)
   car.moves = transition.to (car, {time = 3000,  x=display.contentWidth-30 , y=math.random(0,display.contentHeight), onComplete = hitWall } )
   if car == "carblue.png" then
    car.id = "blue"
   else
    car.id = "normal"
   end
   car:addEventListener ( "tap", touchCar )

   return car

   end

 function touchCar(event)
    local cartouched = event.target
    transition.cancel ( event.target.moves )
    audio.play(brake)
    cartouched:rotate (370)

   if  event.target.id == "blue" then
      score = score + 100
   else
      score = score +5
   end
    scorelabel.text = "Score: ".. score 
    local function removeCar()
    display.remove(cartouched)
  end 
 timer.performWithDelay(600, removeCar)
 createCar()
 end