[Resolved] Some help for a beginner?

Hi all,

I’ve just started working with the SDK while having no programming experience except a few weeks of ‘Code Year’.

I started prototyping a game mechanic idea that I had using Gamesalad but I kept thinking that a better long-term approach would be try and learn coding as I go along using Corona.

That original prototype can be seen here: http://itssinclair.com/prototyping-part-2/ -

and this is the thinking behind it: http://itssinclair.com/a-game-concept/

I’ve already started to try and get something like that working with Corona, but I’m struggling already. I hope there’s someone here willing to help me get over this little mountain:

I want to tap my ‘player’ to ‘charge’ it up, then tap anywhere on the screen and make that object teleport to that area but I’m having only partial success - I can either tap the player object and ‘charge’ it up (switch image, and change the 'charge variable from 0 to 1), or I can simply teleport my object around the screen in accordance with my taps. BUT I can’t get these two functions to play nice together.

Here’s my code (set to teleport only):

[lua]-----------------------------------------------------------------------------------------

– main.lua


–screen bootstrap
display.setStatusBar(display.HiddenStatusBar)
local whiteBg = display.newRect(0,0,1024,768)

local player = display.newImage(“images/player.png”)
player.x = 200
player.y = 384

local enemy1 = display.newImage(“images/enemy1.png”)

–groups
playerGroup = display.newGroup()
enemyGroup = display.newGroup()

playerGroup:insert(player)
enemyGroup:insert(enemy1)
– upon direct tap, player image switches, and ‘charge’ state is set to 1
charge = 0
local function chargeUp(event)
local saveX = event.target.x
local saveY = event.target.y
if(charge == 0) then
player:removeSelf()
player = display.newImage(“images/charged.png”)
player.x = saveX
player.y = saveY
charge = 1
print(charge)
end
end
–player:addEventListener(“tap”, chargeUp)

– upon tapping anywhere on the screen while ‘charge’ is set to 1, the player image
– will teleport to the tapped location
local function teleport(event)
local teleportX = event.x
local teleportY = event.y
–if(charge == 1) then
player:removeSelf()
player = display.newImage(“images/player.png”)
player.x = teleportX
player.y = teleportY
charge = 0
print(charge)
–end
end
whiteBg:addEventListener(“tap”, teleport)[/lua] [import]uid: 121833 topic_id: 25790 reply_id: 325790[/import]

Hey there,

[lua]display.setStatusBar(display.HiddenStatusBar)
local whiteBg = display.newRect(0,0,1024,768)

local player = display.newImage(“hat.png”)
player.x = 200
player.y = 384

local enemy1 = display.newImage(“smile.png”)

–groups
playerGroup = display.newGroup()
enemyGroup = display.newGroup()

playerGroup:insert(player)
enemyGroup:insert(enemy1)

– upon direct tap, player image switches, and ‘charge’ state is set to 1
charge = 0
local function chargeUp(event)
print (charge)
if charge == 0 then
local saveX = event.target.x
local saveY = event.target.y
if(charge == 0) then
player:removeSelf()
player = display.newImage(“smile.png”)
player.x = saveX
player.y = saveY
charge = 1
print(charge)
return true
end
end
end
player:addEventListener(“tap”, chargeUp)

– upon tapping anywhere on the screen while ‘charge’ is set to 1, the player image
– will teleport to the tapped location
local function teleport(event)
if charge == 1 then
local teleportX = event.x
local teleportY = event.y
–if(charge == 1) then
player:removeSelf()
player = display.newImage(“hat.png”)
player.x = teleportX
player.y = teleportY
player:addEventListener(“tap”, chargeUp)
charge = 0
print(charge)
end
end
whiteBg:addEventListener(“tap”, teleport)[/lua]

Try that - obviously after putting your own images back. (I only had two pngs, a hat and a smiley face, to hand - so that’s why I changed it to test.)

Peach :slight_smile: [import]uid: 52491 topic_id: 25790 reply_id: 104398[/import]

Awesome! Thank you.

I took a look at your changes/additions and I’m wondering WHY it works!

What is the purpose of ‘return true’ and why did you put the tap listener inside the function?

I really appreciate the help and your time.

Thanks again. [import]uid: 121833 topic_id: 25790 reply_id: 104889[/import]

Hey there,

return true is used to stop events propagating. If you have two objects on top of each other and both have tap/touch events but you only want the top one to react then by putting return true at the end of the function will do that.

The listener I added inside that function because you recreate the player and therefore it has no event listeners. You have to add it so you can teleport around again with the new player object.

Glad I was able to assist, marking as resolved :slight_smile: [import]uid: 52491 topic_id: 25790 reply_id: 105016[/import]