It isn’t a big, exciting part of the mini series but it is new and it does cover some very, VERY handy things you will all use at some point while making iPhone apps with Lua and the fabulous Corona SDK.

Below, as included in a readme.txt file in the download, is a brief note accompanying this installment in the Corona For Newbies mini series;

Thanks for downloading part three of Techority’s mini series, Corona for Newbies.

Today we’re covering how to drag an object, how to keep an object within the confines of your screen and how to put these things to practical use.

There is a lot more I’d like to cover, but that can wait until part 4 – I wanted to give you something to play with in the meantime.

Enjoy! (If you enjoy it enough, you can show thanks by liking Techority on Facebook or making a small donation to the site. $5 feeds a hungry Peach for a day!)

Part 4 will be out soon, so leave your suggestions at Techority.com for me

Peach Pellen

http://twitter.com/peachpellen

I hope you will enjoy Corona For Newbies Part 3, please leave requests for part 4 below, or email me.

Peach Pellen

PS – If you haven’t already, check out my templates! There’s a big, obvious button on the right hand side of the page!

Hey Adrian, you are correct – I elaborate more on that in other tutorials however for the 4 original Corona For Newbies sets I deliberately broke things up as much as possible. People who have never programmed before seem to have an easier time memorizing and understanding code when it is all broken up in such a way. (I noticed on your site you’ve got some experience coding; more than me from the looks of things ;))
In any case, you’re right – and that’s my reasoning for not doing so in this particular tut
Thanks for the kind words.

I checked out your tutorial and it makes sense now why you had those extra checks to make sure the objects don’t collapse into one position when you touch them as I was trying to create something similar. Any hints/tips on creating move on an object with some delays? The objects in your example will move to the exact position were your finger is touching, but I want to have a delay / some fluidity to it before reaching the position where your finger is pointing. I saw this feature on an app and wanted to do the same. Please get back to me when you’re rested and get some time.

Hi Peach, I’ve gone through all of your tutorials, and now I am going through them again. I bought a 2nd ed prog in Lua book, the reference manual etc.

Now I am really starting to tear things apart and I have a question

In the following code example, I display a box on screen and I drag it around. That’s fine and dandy.

What happens is when I touch the EDGE of the box, the box CENTERS on my mouse cursor in the simulator. I haven’t bought a Corona SDK license yet as I am waiting until I have enough of a app/game to actually put out there.

How would I just drag the edge of a box without centering on my cursor (or on a piece of hardware, a finger/thumb)?

This is from your part 3, Noobies guide to Corona, 1. Simple Dragging.

I also commented the code to the extreme to get MY understanding of how this works in my own words. Am I on the right track with how the function, drag stuff works?

–[[
The local function we define with a name of choosing - in this case that's 'dragbox'
then we put in (event) *explained more below*
Then have to take the local variable we created earlier 'local boxpic' (which is what represents the box.png)
and then define how and when to track it. The how to track part is:

boxpic.x = event.x
boxpic.y = event.y
end

So basically what we are saying is I want to track the picture on whatever X and Y coordinates the picture is dragged to
the = event.x and event.y are tied to the "addEventListener"

Then we are defining what kind of event listener? in this case its "Touch", then we call the local function 'dragbox'
"touch" is on boxpic, we want to match the event coordinates.

What I understand about a touch even, is the event.x and event.y will follow the location of the touch -
>>>as long as it is on the boxpic!!!!!
--]]

I’m about to go back to bed (damn insomnia!) so I’m not totally with it right now; but is what you’re asking basically “How do I go to a gameover (or whatever) screen and on that screen display three different numbers, matching how many times my three things were touched?”

Gotcha…thanks.
Sooo…..do ya have any examples of how I could have the scores all print onto a different ‘page’?
For example, after touching the left, right & middle ‘x’ number of times, (using Director, of course), go to a different/next page to see the totals? :-}

I got the event listener to work for each item touched (versus the background)…how can I keep track of the individual touches to each object versus a total score? Say I’ve got 3 items on the screen – I’d like to keep track of how many touches I make to the left vs middle vs right object.

I solved the blue balloon problem finally! Plus, the red balloon one that didn’t affect much on the terminal (Although it probably would’ve screwed everything up If I tried it on an iDevice).

For the blue balloon it was a scope and recursive problem. First off,

1

local function function_name()

Does NOT equal:

<local function_name = function()

for recursive functions.

I know this because changing that ends up screwing me over. It seems using the first one, the shortcut, ends up defining things in a slightly different order, causing scope problems in very rare occasions. So, I swapped to the second way, after moving some stuff around, doing a few forward declarations, altering some local/global stuff, and TADA! Works like a charm…

Except the fact that after death and you fade to the next screen it seems to have tons of errors that have no permanent affect. Odd. I think I’m forgetting to remove something, but I’ll fix it later.

Anyways, I have to say, THANK YOU! You’ve been a great help. I never even thought about canceling the transition before removing the object, even though it was right in front of my face. Dx

Well, that’s another obstacle or two cleared, and more knowledge for it.

Yeah, I’ll probably get an account on Ansca so I can use the forums when I get the energy to stop being lazy. The pop function is indeed within the spawn function. Which is why I’m saying my code is messy in a way, I always seem to have a hundred functions withing functions calling functions, and then wonder why there’s an error. e_e

Thanks for taking the time to try and help me. xD I know it must be annoying to have me post a thousand walls of text, and then be confronted by them as soon as you wake up. O:

Ok, it IS the transition most likely, and maybe the timer even. I noticed I’m not using local when defining variable timers and transitions. So, I tried to cancel it but it canceled all of my balloon transitions. I tried adding local but now it says it’s nil because it can’t find it. That’s really odd though because of where I defined them, it should be within scope.

I define the timers and transitions right after defining the balloon and it’s functions, I even tried defining them at the same time because defining them at the same time doesn’t seem to screw up the functions at all. Still can’t use local. I find that odd, I define three variables; a balloon, a timer, and a transition, yet I can only access the balloon. I’m kind of stumped. I’m guessing I’m somehow supposed to pass the timer & transition i want to cancel to the Pop function and Cull functions then?

I guess I could use a timer with a one millisecond delay to call another function… Although I don’t see how it would help. I’ve looked at the code several times and it doesn’t seem to have anything wrong with it. Although another guy had a similar problem iirc, in the end it had some odd thing to do with looping, he also couldn’t use removeSelf from a table, the object directly, group etc. I tried collecting garbage after nil’ing out the balloon with a timer (no delay with timer for collection, oddly) but it doesn’t seem to help.

It is in transition when I remove it in my pop function, do you think that could be the problem? All though I’m not sure why that’d call my Cull function even after the timer that calls it, the only caller, is removed.

Thanks. I am using physics for another type of balloon already, and I need the gravity to stay upwards. As far as I know gravity can’t be assigned to individual objects separately, so I decided to use a transition. Originally I used ‘onComplete=cull’ but guess what happened? Not only did the HP not go down and the cull function wrecked whenever I got that error, but the transition would stop randomly! Seriously. After popping a balloon, the next one would immediately glitch and stop at a random point on screen.

I’ve thought about adding physics to the blue balloons and as you said, adding a collision detector, but they’d collide with my red balloons going upwards. I could assign a bit to filter collisions, but that’s getting more complicated than I want to get. I’m thinking about reverting back to an ‘onComplete’ call, and just living with the random stopping. Make it a gameplay feature or something.

I don’t know, what’s better?

1. Letting the blue balloons move unimpeded, but half of them never removing themselves or lowering hp when they got across the screen…
or 2. Letting the glitched ones stop on a random spot on screen so that they’d at least be pop-able.

Also, the red balloons get the same problem, but their HP code almost always works and they don’t stop moving. I think I need to use more tables and arrays, but I have some trouble grasping the syntax.

I know that this:

123456789

-- Defines table
tablename = { }

-- Sets values
tablename = {1, 7, 8, blah, orange, purple, whatever}

-- Retrieves values, [#] determines place

variable = tablename[1]

And that’s it. I fail when it come to making any practice use out of them, especially in handling functions. As you can see, I just kind of slap on functions within function within functions as things spawn… Of course this causes scope problems, etc.

—

Also, I know that physics don’t work between children of different display groups; is there anyway to make two groups affect each other? I’m thinking of making a platformer once I get better, but one thing stumps me: camera. I figured I’d put every tile into a ‘camera’ group as it spawned, and move the whole stage left and right as the character moves. This obviously would not work. If I made the player and other not movable objects into another group, and put both the camera and player groups into another group, would they affect each other then?

I’m guessing not, because now they’re just in separate groups within a group. Another option would be to make a physicsGroup in which everything that was to interact with each other would be added, besides other groups.

It’s OK, your code isn’t too messy; you should see the stuff of mine I don’t post, haha.

Now, if it works with your game, I’d suggest using physics – have a wall out of sight that will kill any balloons on hit.

Alternatively, if you want to avoid physics there is likely something that could be put together – I’m exhausted right now so not the time to think on it – but if you are interested you could email me, I’ll take a look and see if I can fix it, then if I can – if you’re interested – I’d charge you a small fee. (I’d give you a quote before expecting any sort of commitment.)

This doesn’t pertain to this tutorial directly per say, but I am having a few problems.

I have a function that spawns a balloon every few seconds, and along with the balloon two other functions pertaining to it are created as well; Cull, which removes the balloon when it’s off screen and lowers the player’s HP, and Pop, which pops the balloon and adds to the score when touched. The problem is Cull doesn’t want to work with every other balloon as shown by a print function.

It seems Cull is being called, even after I remove the timer that calls it (I know it is successfully removed by testing earlier.) Of course, it tries to remove the balloon even though it no longer exists, so I tried removing the function as well as removing the balloon but no dice.

The really odd thing is that it:

1. Cull should not be called. (Possible scope problem?)
2. The ‘if’ statement at the begging of the code that dictates whether to continue the function is skipped. (I honestly don’t know how this is even possible!)

I thought the timer that calls the cull function could be calling other balloon’s cull functions just as they are being removed, but as I said I tried nil’ing out the cull function with no result. Nothing I do seems to work. Also, I should be using Tables and Arrays but their syntax and use with them is hard to grasp for me, I’m sure if you made a tutorial on the integration of tables and arrays to aid in handling basic functions many newbies such as myself would be thankful.