[Resolved]Collecting one item to collect another and drop the state after it

Hello Again,
I´m quite in the final steps of my first game but can’t figure out one thing, which is rather important :slight_smile:

I have a set up of different creatures and items which the player catches. I would like to achieve the situation where the player must catch those creatures and items in order or related to each other.

A simple example:
If you catch a bee you lose 10 points, but if you catch a star before it and then catch a bee you gain 10 points + the state is dropped, meaning if you catch another bee you lose 10 points again, unless you catch a star first.

Here would be the code where I drop one item:

[lua]–***************************************************
– BEE CREATION, COLLISION & DROP
–***************************************************

local onbeeCollision = function( self, event )

if (event.other.myName == “tongue” and event.target.myName == “bee” or event.other.myName == “ground”) then
local doCollision = function()
audio.play( bugCaughtSound )
self.isHit = true
print( “bee destroyed!”)
self.isVisible = false
self.isBodyActive = false

eatenBug.x = self.x; eatenBug.y = self.y
eatenBug.alpha = 0
eatenBug.isVisible = true

local fadebug = function()
transition.to( eatenBug, { time=500, alpha=0 } )
end

transition.to( eatenBug, { time=50, alpha=1.0, onComplete=fadebug } )
timer.performWithDelay( 50, resetTongue )

self.parent:remove( self )
self = nil

end

–COLLISION TIMER
local collisionTimer = timer.performWithDelay( 1, doCollision, 1 )

if event.other.myName == “character” then
bugCount = bugCount + 1
bugText.text = "Catched: " … tostring( bugCount )
bugText.xScale = 0.5; bugText.yScale = 0.5
bugText.x = (480 - (bugText.contentWidth * 0.5)) - 15
bugText.y = 305
print(“bee caught”)

local newScore = gameScore + 2
setScore( newScore )
elseif event.other.myName == “tongue” then
bugCount = bugCount + 1
bugText.text = "Caught: " … tostring( bugCount )
bugText.xScale = 0.5; bugText.yScale = 0.5
bugText.x = (480 - (bugText.contentWidth * 0.5)) - 15
bugText.y = 305
print(“bee caught”)

local newScore = gameScore -10
setScore( newScore )

elseif event.other.myName == “ground” then
print(“Ground hit!”)
end

end
end

–BEE DROP
local beeDrop = function()
local displayScale = display.contentScaleX

if displayScale < 1 then
beeSheet = sprite.newSpriteSheet( “images/beeSprite@2x.png”, 130, 120 )
else
beeSheet = sprite.newSpriteSheet( “images/beeSprite.png”, 65, 60 )
end

local spriteSet1 = sprite.newSpriteSet(beeSheet, 1, 2)
sprite.add( spriteSet1, “move”, 1, 2, 100, 0 )
bee = sprite.newSprite( spriteSet1 )

if displayScale < 1 then
bee.xScale = .5; bee.yScale = .5
end

bee.x = display.contentWidth / 2
bee.y = display.contentHeight / 2
bee:prepare(“move”)
bee:play()

bee.x = mRand( 80, 450 ); bee.y = -40
transition.to( bee, { time=mRand(1500, 2000), y = 400, transition=easing.linear } )
bee.isHit = false

physics.addBody( bee, “dynamic”,{ density=beeDensity, bounce=0, friction=0.5, shape=bugShape } )
bee.isFixedRotation = true
bee.myName = “bee”
gameGroup:insert( bee )

bee.postCollision = onbeeCollision
bee:addEventListener( “postCollision”, bee )
counter:toFront()
pauseBtn:toFront()
scoreText:toFront()
bugText:toFront()
end

– CREATE NEW BEE
local beeTimer = function()
startDrop = timer.performWithDelay( mRand(1000, 1500), beeDrop, 0 )
end[/lua]
Any help or hints would be super!

Thanks,
Gert
[import]uid: 105289 topic_id: 30558 reply_id: 330558[/import]

I really like to see a question mark.

If i understood the problem, you can save in a table each collision.

In star collision save in the table=[#table + 1] = “star”, and in the onbeeCollision, ask if the last row of that table is a star. If it’s, +10 points. [import]uid: 116842 topic_id: 30558 reply_id: 122473[/import]

You could just have 2 separate bee spawns with 2 separate timers?

beeDrop
beeDropNeg

beeDrop would cancel/pause beeTimer on collision and start beeTimerNeg, to spawn bees with negative points. [import]uid: 114389 topic_id: 30558 reply_id: 122524[/import]

Thanks for the replies. I think correogracioso solution would work but maybe you could give an example, I can’t figure out how to implement it.

The second one has a nice idea, but I think it would not work as needed in my case, because there might be one or more objects in screen already.

I made an example.

If you catch 2 or 3 you will lose 10 points (the bees are generated by random, so there might be more, not only one or two)

If you catch 1 and then 2 or 3, you will gain points and after that it looses it´s effect, meaning you can only catch 2 or 3, not both and have to wait for another 1 to gain points again.

I hope it makes more sense now.

Thanks!
[import]uid: 105289 topic_id: 30558 reply_id: 122594[/import]

I don’t like this type of help but…

Declare last_touch = “”

( I imagine you have onhoneypotCollision or onstarCollision )

In onhoneypotCollision write:
last_touch = “honeypot”

In beeCollision ask wichone was the last touch, and if it’s a honeypot give him points, if it isn’t - 10.

With this you can make other combinations ( combos ), like if the last one was other bee, you don’t take -10, u’ll take -20 for “beeing” ( haha, i’m awesome ) a noob. :slight_smile:

PS: Are you the artist, the drawer, painter or whatever you want to call it, of that art? [import]uid: 116842 topic_id: 30558 reply_id: 122601[/import]

Thank you so much (and sorry for asking those things for free, maybe we could find a way to compensate your help, because those are really the two final things which keeping me to build the final levels).

It kind of works, I guess I´m still doing something wrong. I added
[lua] local doCollision = function()
audio.play( bugCaughtSound )
self.isHit = true
print( “bug destroyed!”)
self.isVisible = false
self.isBodyActive = false
last_touch = “honeypot” [/lua]

and added the other part to bee collision

[lua] elseif event.other.myName == “tongue” then
bugCount = bugCount + 1
bugText.text = "Caught: " … tostring( bugCount )
bugText.xScale = 0.5; bugText.yScale = 0.5
bugText.x = (480 - (bugText.contentWidth * 0.5)) - 15
bugText.y = 305
print(“bee caught”)
if last_touch == “honeypot” then
local newScore = gameScore + 2
setScore( newScore )
else
local newScore = gameScore - 2
setScore( newScore )
end[/lua]
It works nice before the first honeypot is caught, but after that always the local newScore = gameScore + 2 is added, no matter if the honeypot is caught first or not.

About the art, this one is not mine, we are doing the game with my friend, he made 90% of the graphics.
At first we started out with a programmer also, but there was a personal tragedy and… Well I was left with the coding part myself. Actually I have made quite far now, thanks to Corona and the forum here, but as I told, a few issues still :slight_smile: [import]uid: 105289 topic_id: 30558 reply_id: 122619[/import]

Please, say to me that you wrote last_touch = “clock” or last_touch = “star”. PLEASE. [import]uid: 116842 topic_id: 30558 reply_id: 122620[/import]

And i’m sorry, i just don’t like to write code for answers. I wish i have a better english to explain me better :frowning:
I prefer showing you the way to get your code done. I’m so freak :wink:

PS:
You have to “free” ( = “” ) the variable after adding the score. [import]uid: 116842 topic_id: 30558 reply_id: 122621[/import]

it´s working!!
Thank you so so much! [import]uid: 105289 topic_id: 30558 reply_id: 122622[/import]

No problem. Test Drivers United! [import]uid: 116842 topic_id: 30558 reply_id: 122623[/import]

I really like to see a question mark.

If i understood the problem, you can save in a table each collision.

In star collision save in the table=[#table + 1] = “star”, and in the onbeeCollision, ask if the last row of that table is a star. If it’s, +10 points. [import]uid: 116842 topic_id: 30558 reply_id: 122473[/import]

You could just have 2 separate bee spawns with 2 separate timers?

beeDrop
beeDropNeg

beeDrop would cancel/pause beeTimer on collision and start beeTimerNeg, to spawn bees with negative points. [import]uid: 114389 topic_id: 30558 reply_id: 122524[/import]

Thanks for the replies. I think correogracioso solution would work but maybe you could give an example, I can’t figure out how to implement it.

The second one has a nice idea, but I think it would not work as needed in my case, because there might be one or more objects in screen already.

I made an example.

If you catch 2 or 3 you will lose 10 points (the bees are generated by random, so there might be more, not only one or two)

If you catch 1 and then 2 or 3, you will gain points and after that it looses it´s effect, meaning you can only catch 2 or 3, not both and have to wait for another 1 to gain points again.

I hope it makes more sense now.

Thanks!
[import]uid: 105289 topic_id: 30558 reply_id: 122594[/import]

I don’t like this type of help but…

Declare last_touch = “”

( I imagine you have onhoneypotCollision or onstarCollision )

In onhoneypotCollision write:
last_touch = “honeypot”

In beeCollision ask wichone was the last touch, and if it’s a honeypot give him points, if it isn’t - 10.

With this you can make other combinations ( combos ), like if the last one was other bee, you don’t take -10, u’ll take -20 for “beeing” ( haha, i’m awesome ) a noob. :slight_smile:

PS: Are you the artist, the drawer, painter or whatever you want to call it, of that art? [import]uid: 116842 topic_id: 30558 reply_id: 122601[/import]

Thank you so much (and sorry for asking those things for free, maybe we could find a way to compensate your help, because those are really the two final things which keeping me to build the final levels).

It kind of works, I guess I´m still doing something wrong. I added
[lua] local doCollision = function()
audio.play( bugCaughtSound )
self.isHit = true
print( “bug destroyed!”)
self.isVisible = false
self.isBodyActive = false
last_touch = “honeypot” [/lua]

and added the other part to bee collision

[lua] elseif event.other.myName == “tongue” then
bugCount = bugCount + 1
bugText.text = "Caught: " … tostring( bugCount )
bugText.xScale = 0.5; bugText.yScale = 0.5
bugText.x = (480 - (bugText.contentWidth * 0.5)) - 15
bugText.y = 305
print(“bee caught”)
if last_touch == “honeypot” then
local newScore = gameScore + 2
setScore( newScore )
else
local newScore = gameScore - 2
setScore( newScore )
end[/lua]
It works nice before the first honeypot is caught, but after that always the local newScore = gameScore + 2 is added, no matter if the honeypot is caught first or not.

About the art, this one is not mine, we are doing the game with my friend, he made 90% of the graphics.
At first we started out with a programmer also, but there was a personal tragedy and… Well I was left with the coding part myself. Actually I have made quite far now, thanks to Corona and the forum here, but as I told, a few issues still :slight_smile: [import]uid: 105289 topic_id: 30558 reply_id: 122619[/import]

Please, say to me that you wrote last_touch = “clock” or last_touch = “star”. PLEASE. [import]uid: 116842 topic_id: 30558 reply_id: 122620[/import]

And i’m sorry, i just don’t like to write code for answers. I wish i have a better english to explain me better :frowning:
I prefer showing you the way to get your code done. I’m so freak :wink:

PS:
You have to “free” ( = “” ) the variable after adding the score. [import]uid: 116842 topic_id: 30558 reply_id: 122621[/import]

it´s working!!
Thank you so so much! [import]uid: 105289 topic_id: 30558 reply_id: 122622[/import]

No problem. Test Drivers United! [import]uid: 116842 topic_id: 30558 reply_id: 122623[/import]