Count 'touchs' points

Hi guys.
I need some help. Look at the code below:

[lua]local removeRocks = function( event )
local t = event.target
local phase = event.phase

if “began” == phase then
t:removeSelf()
end
return true
end[/lua]

Ok, this is easy to figure out. When you touch the screen the target will be removed. That’s ok. But I want to make a counter to every target removed, so when you touch down a target the counter on the top of the screen will update as so on you touch many targets.
My objective is to do a “point counter” to every target.
Every target gives you +5 points and the counter update every touch with new values depending on many targets you have removed.

Thanks!
[import]uid: 118103 topic_id: 21702 reply_id: 321702[/import]

Hello henrique1,

Here’s a little plug -n- play for ya to start.
just copy and paste in a new main.lua file and go…:wink:

\_W = display.contentWidth;  
\_H = display.contentHeight;  
mRand = math.random;  
  
total\_score = 0;  
total\_score\_add = 5;  
total\_rocks = 10;  
local display\_score = display.newText(total\_score, 0, 0, native.systemFont, 32\*2)  
 display\_score:setReferencePoint(display.TopLeftReferencePoint);  
 display\_score.xScale = 0.5;  
 display\_score.yScale = 0.5;  
 display\_score.x = 25;  
 display\_score.y = 25;  
  
  
local function addScore(event)  
  
 total\_score = (total\_score + total\_score\_add);  
 display\_score.text = total\_score;  
  
end   
  
  
local function trackRocks(obj)  
  
 obj:removeSelf();  
 addScore();  
  
  
end  
local function spawnRock()  
  
 --from Corona API docs  
 rock = display.newRect(0, 0, 50, 50);  
 rock:setReferencePoint(display.CenterReferencePoint);  
 rock.x = mRand(50,\_W-50);  
 rock.y = mRand(50,\_H-50);  
 rock.strokeWidth = 3;  
 rock:setFillColor(140, 140, 140);  
 rock:setStrokeColor(180, 180, 180);  
  
  
 function rock:touch(event)  
  
 if event.phase == "ended" then  
  
 trackRocks(self);  
  
 end  
  
 -- return true to only remove one rock if overlaps other rocks ;)  
 return true;  
 end  
  
  
 rock:addEventListener("touch", rock);  
  
end  
tmr = timer.performWithDelay(100, spawnRock, total\_rocks);  

Here’s the tutorial on how to do some of this…very good!
http://www.youtube.com/watch?v=6TsDdLY7VXk

Hope this will get you headed in the direction you want
to go…:wink:
Happy Coding

Larry [import]uid: 107633 topic_id: 21702 reply_id: 86130[/import]

[lua]local count = 0
local myText = display.newText(“0”, 10, 10, native.systemFont, 24)

local removeRocks = function( event )
local t = event.target
local phase = event.phase

if “began” == phase then
t:removeSelf()
count = count + 5
myText.text = count
end
return true
end[/lua]

That should do it :slight_smile: [import]uid: 52491 topic_id: 21702 reply_id: 86135[/import]

Too slow, apparently :wink: [import]uid: 52491 topic_id: 21702 reply_id: 86136[/import]

Thanks a lot guys!
This helped me so much.

Oh, before I forget, there is anything else:
* How can I change text color?
* How can I ‘change’ the image when I touch a object(in this case the target)? [import]uid: 118103 topic_id: 21702 reply_id: 86144[/import]

Text color, assuming your text is called “myText” like above, is done like this;

[lua]myText:setTextColor(R, G, B)[/lua]

With R being red, G being green and B being blue, all values from 0 to 255.

For “changing” the image - you don’t. Not exactly, it isn’t that simple.

You can either use a sprite sheet for the image and set the frame OR you remove the image and add a new one in its place. (This is effectively instant and not detectable to anyone playing the game.)

Peach :slight_smile: [import]uid: 52491 topic_id: 21702 reply_id: 86269[/import]

Hi Peach,

Thanks for answering. Can you explain how can I do this?
Henrique. [import]uid: 118103 topic_id: 21702 reply_id: 86293[/import]

Hello again,

Thanks Peach for helping out, but your never to slow… :slight_smile: :wink:

Henrique,

Here’s some more simple code for ya to try, it’s one way
of doing things quick and easy to understand.

Depending on what your trying to do, or what end results
your after, it should help you along the way to your goal…
hopefully…:wink:

  
\_W = display.contentWidth;  
\_H = display.contentHeight;  
mRand = math.random;  
  
total\_score = 0;  
total\_score\_add = 5;  
total\_rocks = 10;  
  
-- text colors here  
R = 255; -- a number from 0 - 255  
G = 0;  
B = 0;  
local display\_score = display.newText(total\_score, 0, 0, native.systemFont, 32\*2)  
 display\_score:setReferencePoint(display.TopLeftReferencePoint);  
 display\_score.xScale = 0.5;  
 display\_score.yScale = 0.5;  
 display\_score.x = 25;  
 display\_score.y = 25;  
 display\_score:setTextColor(R, G, B)  
  
  
local function changeRock(obj)  
 -- we are actually going to change the rock  
 -- NOT just change the color  
  
 -- dont' need this line if using images  
 local newRock = obj; -- copy obj (rock) to newRock before we remove the 1st one  
  
 -- use this line if using images  
 --local newRock= display.newImageRect("myBack.jpg",20,20);  
 -- you will have to set the x and y if your using  
 -- images...I left some homework for ya... ;)  
 obj:removeSelf(); -- remove 1st rock (obj) / image  
  
 -- use this line only if you are displaying images  
 newRock = display.newRect(newRock.x - 25, newRock.y - 25, 50, 50);  
 -- just to see remove the - 25 from x,y ABOVE and see if  
 -- this is a NEW rock  
  
 --don't need if using images  
 newRock:setFillColor(140, 0, 140)  
  
  
 return true;  
end  
local function addScore(event)  
  
 total\_score = (total\_score + total\_score\_add);  
 display\_score.text = total\_score;  
  
  
 return true;  
  
end   
  
  
local function trackRocks(obj)  
  
 --obj:removeSelf(); -- we will remove later in changeRock(obj) function  
 changeRock(obj);  
 addScore();  
  
  
 return true;  
end  
local function spawnRock()  
  
 --from Corona API docs  
 rock = display.newRect(0, 0, 50, 50)  
 rock:setReferencePoint(display.CenterReferencePoint);  
 rock.x = mRand(50,\_W-50);  
 rock.y = mRand(50,\_H-50);  
 rock.strokeWidth = 3;  
 rock:setFillColor(140, 140, 140);  
 rock:setStrokeColor(180, 180, 180);  
  
  
 function rock:touch(event)  
  
 if event.phase == "ended" then  
  
 trackRocks(self);  
  
 end  
  
 -- only remove one rock if it overlaps other rock(s)  
 return true;  
 end  
  
  
 rock:addEventListener("touch", rock);  
  
end  
tmr = timer.performWithDelay(40, spawnRock, total\_rocks);  

…Happy Coding
Best Regards,
Larry [import]uid: 107633 topic_id: 21702 reply_id: 86301[/import]

Thanks Larry - great example.

You’ve made a few helpful posts I’ve noticed lately; very cool to see!

Peach :slight_smile: [import]uid: 52491 topic_id: 21702 reply_id: 86305[/import]

Hello Peach,

No problem…:slight_smile: just helping where I can…and glad to
do it…

We need Better Docs tho, :wink: and more…books…on
Corona…:slight_smile:

But, it is fun getting someone along further and seeing that
they are learning…that’s a pretty good feeling…:wink:
Thanks Again,

Larry
[import]uid: 107633 topic_id: 21702 reply_id: 86377[/import]

We know the docs need some work, trust me :wink:

As to books, well, there’s a couple now - and I’m working on one myself but it’s not exactly a fast process.

I agree it is fun helping someone move forward and learn; it’s what I seem to spend most of my own time doing and I certainly enjoy it.

Either way, really awesome to see - keep it up!

Peach :slight_smile: [import]uid: 52491 topic_id: 21702 reply_id: 86422[/import]

Hi guys!

Thank you so much! But I’ll try to explain more details about what I am trying to do, then you can help me more efficiently.

So, let’s start.

I have an object and we will can it’s “Object1”. This is static.
The rocks we will call “rock1, rock2 and rock3”.
These rocks are failing down to the “object1” and the player objective is to smash down these rocks before they get to the “object1”. When a player touch a rock, the rock remove itself. (the code’s just above.)

But there is no effect in this “remove rock”. I want them to explode or something like this(I can design the explosion in spritesheets or something else that I could use for that)

There is more: when the rock reaches (collide) with “object1” I want it to change its form. (Object1)

Let’s figure out with an exemple:
There are three rocks falling down, I could touch at least two and so they explode and remove.itself() after the effect of explosion. The other rock smashes with “object1” and causes “object1” to change its form(like sprite, too).

I’ve five images for “object1”. As so on the rocks collide with it, it will change its form.

I’ve recorded a video last friday, take a look so you guys can help me:
http://www.screencast.com/t/xSgBZxIy5oB

Thank you so much!
my english sucks, any problems please, post here and I will try to explain/give detailed information
Once again, Thanks!

[import]uid: 118103 topic_id: 21702 reply_id: 86698[/import]

For the explosion, etc. you could use a sprite and in the function where you remove the rock, right before you remove it, spawn and play the sprite at the rock.x and rock.y.

For changing the sun image/form when it gets hit, you would also make it a sprite - then when hit you could say sun.currentFrame = 2 and it would change to the second image in the sheet.

Does this make sense?

Peach :slight_smile: [import]uid: 52491 topic_id: 21702 reply_id: 86786[/import]

Hiiiii peach!

Sorry for taking too long to answer the thread. I had some problems with my studies.

So, let’s go. Now I have a ideia to how-to do these things. But what’s about practice? I have no idea how to implement these things you have taught me. Can you give me a example code of these features in use?

It will help me a lot.
#Explosion
Can you give me more detailed information about it?
I understood that I’ve to make a explosion effect (calling the function of the spritesheet of the explosion sprite on rock.x and rock.y right before I remove it, how can I implement a spritesheet outside of the main file?)

#Stuff
I’ve already turned sun images into a spritesheet.

Soon I’ll have another questions about vectors and images(if you play again the video you’ll see that the sun image is a square although transparent, can’t be!). Hope you can help me as you’ve done well this far. Thanks, many many thanks! [import]uid: 118103 topic_id: 21702 reply_id: 87344[/import]

Hey again,

This line confused me; “how can I implement a spritesheet outside of the main file?”

Are you using director/storyboard or is all your code in main.lua? You would set up the sprite in the file you planned on using it in, it would not have to be main.

If you can elaborate further I can try to advise :slight_smile:

Peach [import]uid: 52491 topic_id: 21702 reply_id: 87425[/import]

Hi peach,

I wanna use multiple files and call them in the main file.
(a file to a spritesheet function, a file to button function…)

How to create spritesheets?

Thanks. [import]uid: 118103 topic_id: 21702 reply_id: 87655[/import]

Ah I see, you want to know about modularization - take a look at Beebe’s article here; http://blog.anscamobile.com/2011/09/tutorial-modular-classes-in-corona/

:slight_smile: [import]uid: 52491 topic_id: 21702 reply_id: 87778[/import]

Hi Peach!

Thanks, thanks and thanks! This helped me a lot!!!

Now, how about:
“questions about vectors and images(if you play again the video you’ll see that the sun image is a square although transparent, can’t be”

Many thanks!
You’ve already helped a lot!

Hugs.
[import]uid: 118103 topic_id: 21702 reply_id: 87842[/import]

Oh the physics body is square - yeah. You should set a radius for a circular body instead;

[lua]physics.addBody(sun, {radius=50})[/lua]

Obviously with other physics info in there too :wink:

Thanks for the hugs! :smiley: [import]uid: 52491 topic_id: 21702 reply_id: 87993[/import]

Yeaaaaaaaaaaaaaaaaahaaaaaaaaaaaaaaa!
Many Thanks Peach!

We’re solved.
Hugs. [import]uid: 118103 topic_id: 21702 reply_id: 88073[/import]