How to make a goal area?

Hello,

I am new to using Corona and I’ve been creating a simple game to test out both my own and the software’s abilities. So far, I’m loving the program, but I’m afraid I’m stuck. I have this soccer ball and a platform at the top left and I want to know how can I make that platform trigger a “Win” or “Congratulations” message when the ball lands on it. What I’m thinking of is kind of like the popular game Bubble Ball: when the ball reaches the flag, you’ve won the level. I am using this sample as a base: http://www.fantomgl.com/files/JuggleSoccerBall.zip (download)

I’m not sure how to post an image here, but it’s the same thing as the sample code above but with a small platform on the top left; almost to the middle left. The objective is to tap the ball until you can get it unto the platform and “win” the level.

Thank you in advance,
technologygeek01

P.S. Excuse me for sounding like a noob, but I’ve seen the community here help so many other people and thought to myself, “Why not?” :slight_smile: [import]uid: 57808 topic_id: 9733 reply_id: 309733[/import]

Hi and welcome! :smiley:

You could do this a few different ways. The easiest is probably to give the ball/end platform a collision listener; on collision with the other you’d call the endlevel (or whatever you’re calling it) function.

I hope that helps; you can find out more about Collision detection in the Sample Code section of the website.

Peach :slight_smile: [import]uid: 52491 topic_id: 9733 reply_id: 35480[/import]

I’m sorry. Could you be a bit more specific, because I’m kind of lost. :-S [import]uid: 57808 topic_id: 9733 reply_id: 35515[/import]

Apologies :slight_smile:

http://developer.anscamobile.com/content/game-edition-collision-detection

Basically when the ball makes contact with the platform, you want to know and then use a function to show your message. (Which I assume you know how to do if you’ve gotten this far?)

The example I linked to will print the names of the objects colliding but it can do anything a normal function can; remove an object, spawn something, play a sprite, etc.

Does that help?

Peach [import]uid: 52491 topic_id: 9733 reply_id: 35516[/import]

I think I got it to detect the collision, but I don’t know how to display the message… The link did help a lot, though. I’m so sorry If I’m being a bother. I’m just so eager to learn this! [import]uid: 57808 topic_id: 9733 reply_id: 35518[/import]

did you put the print function inside the collision?

[lua]-- example

local onGoal = function (self, event)
if event.phase == “began” then
– do something here
– ex: create your winning message here
– print to test if the collision is working, check terminal
print(“Collision is working”)
end
end
[import]uid: 12455 topic_id: 9733 reply_id: 35519[/import]

Is there a way to use collisions to add score? [import]uid: 25216 topic_id: 9733 reply_id: 35520[/import]

yep…

like this you mean?

[lua]-- initial value for score
local score = 0

local onGoal = function (self, event)
if event.phase == “began” then
– goal .name can be created on your goal object
– just add this to your goal object
– goal.name = “goal”
if event.other.name == “goal” then
– this will remove the ball
self:removeSelf()

– update the score, for now check the terminal for result
score = score + 1
print("the score is now: "…score)
end
end
end[/lua]

hope that gives you an idea [import]uid: 12455 topic_id: 9733 reply_id: 35522[/import]

As teex84 said, do a print to ensure it’s working.

What do you want to display on collision? Is it new text within Corona or an image of a “you win!” type message?

You aren’t at all a bother :slight_smile: Corona is awesome and it’s great you’re learning new things and asking questions!

DLuksa - yes. See that code teex84 pasted? replace the print function with your score changing function. Eg;
score = score + 10

Peach :slight_smile:

*Edit: I was beaten to it on the score count. D’oh! [import]uid: 52491 topic_id: 9733 reply_id: 35523[/import]

What do you mean? [import]uid: 57808 topic_id: 9733 reply_id: 35524[/import]

I want to display a “You win” image. When the ball touches the goal the terminal says something like, “Soccerball: collision began with goal,” so I guess that means the collision is working… [import]uid: 57808 topic_id: 9733 reply_id: 35526[/import]

Where you have the print line, add;

[lua]local youwin = display.newImage “youwin.png” – Obviously this is your image
youwin.x = 150 – change as needed
youwin.y = 150 – change as needed[/lua]

See? Just like adding an image in the normal way; just do it next to your print line. [import]uid: 52491 topic_id: 9733 reply_id: 35528[/import]

So, say that there are different colored balls with different score, how would I attach that score to the object? [import]uid: 25216 topic_id: 9733 reply_id: 35529[/import]

now that’s getting more interesting.
You can add if statement inside your collision function.

if this do that… if that do this…

[lua]-- setup your score.
– Note: I haven’t tested this, so i hope it works
– setup the initial value for the game score
local score = 0

local onGoal = function (self, event)
– in the collision, add the if statements
– i’m assuming that you’ve already created the different type of balls

– setup a new variable to add to score
local vScore = 1

if event.phase == “began” then
if self.name == “blue” then
vScore = 5
elseif self.name == “red” then
vScore == 10
else
vScore == 1
end
score = score + vScore

– add anything else you wanted to happen and you’re done
end
end
[/lua]

Like I said, I haven’t tested this code, so I hope it works. If self.name doens’t work, try ball.name.

good luck
[import]uid: 12455 topic_id: 9733 reply_id: 35534[/import]

Oh… I figured that it would look something like that! Thanks for your help! I really appreciate it. [import]uid: 25216 topic_id: 9733 reply_id: 35536[/import]

I finally did it!!! Thanks for the help and support. You guys rock! [import]uid: 57808 topic_id: 9733 reply_id: 35544[/import]

Thanks again, I just have one more question, can I turn simple text
[lua]local score = display.newText( “0”, 460, 10, “font”, 75 )[/lua]
into score? I keep getting stuck up at this part. [import]uid: 25216 topic_id: 9733 reply_id: 35550[/import]

simple text is a very effective way to show score.

Did you take a look at the Ghosts vs Monsters demo yet? There’s a score system in there.

Here’s an example:

Note: I haven’t tested this yet.

[lua]local scoreText = display.newText(“0”, 460,10, “font”, 100)
scoreText.text = “0”
scoreText.xScale = 0.5 – for retina display
scoreText.yScale = 0.5 – for retina display

– this function will update the score when call later in the collision

local updateScore = function (newpoint)
local newpoint = newpoint

scoreText.text = “”…newpoint
end

– in your collision do this
local onGoal = function (self, event)

local vScore = 1

if event.phase == “began” then
if self.name == “blue” then
vScore = 5
elseif self.name == “red” then
vScore == 10
else
vScore == 1
end

score = score + vScore

– feed score into the function
updateScore(score)

end
end

[import]uid: 12455 topic_id: 9733 reply_id: 35559[/import]