Touch Events

I’m trying to port my game over to Corona and it’s taking a little bit of getting used to but not bad so far. I think making menus will be the hardest part.
Any who… One of the elements of my game is dragging a pig around to catch apples. I don’t know the code to make that happen and I was wondering if any of you guys have gone through this before. The two things that I was curious about was,

  1. How do you constrain where you can move the pig
  2. How to you keep it behind other visible objects but in front of the background so that anything that falls, falls in front of you.
    To make the explanation simpler, when an apple falls it is at the front of the screen and in front of the pig.
    I have seen the dragMe tutorial but it didn’t make much sense. I have done some looking around but I can’t seem to grasp the concept. [import]uid: 59140 topic_id: 10198 reply_id: 310198[/import]

Hey there, and welcome!

  1. Are you wanting to know how to drag it? I’m not totally sure - but clarify for me and I’ll get you an answer :slight_smile:

  2. When you spawn an apple, have it move using toFront. (Would you like a link to the API?)

Peach :slight_smile: [import]uid: 52491 topic_id: 10198 reply_id: 37269[/import]

Hi Peach,
Yes. I drag it around but he has to be constrained to the bottom of the screen so that you can’t cheat by getting closer to the apples stationed on the tree.
I would love a link to the API. That is a big part of my game.
I am making 25 levels, do you know if I should just copy the code to all the levels or reference the code. Like when you’re at level 2 do the same code as in level 1?
It’s tough to explain as I don’t know the code phrasing yet.

[import]uid: 59140 topic_id: 10198 reply_id: 37279[/import]

Hey again Dannylego :slight_smile:

First up, here’s a link to the API for toFront; http://developer.anscamobile.com/reference/index/objecttofront

Next - you want to drag it around, but keep it at the bottom of the screen; my question to you is when you say “the bottom of the screen” does this mean that it’s Y is unchanging? You want it to always remain the same height but be draggable left and right, along X?

If you can clarify the above for me I’m happy to advise you on it :slight_smile:

Lastly, different people do different things for multiple levels. I often copy and paste the code, then I change it up a bit in each level as needed.

Others would modularize their code then set up the levels differently that way.

For an example of that, check out the two levels in Ghosts VS Monsters :slight_smile: (A demo available for download in the code exchange.)

Peach :slight_smile: [import]uid: 52491 topic_id: 10198 reply_id: 37427[/import]

Hello,
Yes that is exactly what I want to do. I just want to have movement on the x axis.
Do you know where I might be able to find some info on “for next” loops because I what to duplicate a bunch of apples in random places near the top of the screen.

From looking at the director class I got the impression that when you make different screens, you make different .lua files. Am I correct or not really?

Thanks,

Danny [import]uid: 59140 topic_id: 10198 reply_id: 37455[/import]

Hey Danny,

Check out the Many Crates example in your Corona Sample code folder, under physics - if memory serves that has an example.

RE director, correct! We make different .lua files :slight_smile:

Now - for the X axis, rather than moving it with normal dragging (ie, x = event.x, y = event.y) you’d simply do “pig.x = event.x” and forget the y element all together.

Does this make sense?

Peach :slight_smile: [import]uid: 52491 topic_id: 10198 reply_id: 37585[/import]

Yes that all makes sense to me!

One thing still has been bugging me. I don’t know how to make the pig my “target.” I don’t really know how to attach working scripts to the pig. I was looking at the dragMe sample game and I don’t know how they make it work. As in I’m not sure how they make the squares move around

Also when adding code to the script it keeps giving me a black screen after I’ve added the images.

[code]
display.setStatusBar( display.HiddenStatusBar )
display.newImage ( “apple tree .jpg” )
display.newImage ( “Pig.png” , 130 , 380 )

local function onTouch( event )
local t = event.target

local phase = event.phase
if “began” == phase then

pig.x = event.x

return true

end

[code] [import]uid: 59140 topic_id: 10198 reply_id: 37666[/import]

Hey Peach, disregard that last post. Thanks to your Corona For Newbies - Part 3 tutorial I was up and running with moving my pig around in just a few minutes.

Thanks for your help and tutorials [import]uid: 59140 topic_id: 10198 reply_id: 37834[/import]