Problem changing player character during or after a collision

Hello geniuses, 

Am almost in the process of completing my first animation game but seem to hit a road block.

The game contains several player characters and I  would like for the main player character to change to other characters upon hitting a special powerUp; but that never happens.

In the player module; in the collision function, there is a global variable “playerNum” which I assume should change the player character as well as the animation sequence.

-=====player module============================

–player collision listener function

playerCollision = function(self, event)

          if( event.target and event.other.myType == “stone” )then 

                   if ( event.phase == “began” ) then

                            event.other.isVisible = false

                            event.other.alive = false

                             playerNum = math.random(4)

                             print(“printing playerNum”, playerNum)

                           –  self:setSequence( PLAYER_RUN  )

                           –  self:play()

                            return true

                      end

         end

end 

============================================

Here is the global variable that should change the characters which also should update the animation sequence:

–========global module=============================================

character = {}

character[1] = “First”

character[2] = “Second”

character[3] = “Third”

character[4] = “Four”

runSequence = “run1” 

jumpSequence = “jump”

dieSequence  = “dead1”

shootSequence = “shoot1”

playerNum = 4

–PLAYER IMAGES

PLAYER_RUN = character[playerNum] … runSequence

PLAYER_JUMP = character[playerNum] … jumpSequence

PLAYER_DIE  = character[playerNum] … dieSequence

PLAYER_SHOOT = character[playerNum] … shootSequence

=======================================================================

I dont know which other method to effect this change during a collision.

PLEASE HELP.

Many thanks.

uche.

Hi @uche,

It’s probably not the collision code but instead how you set up the sprite(s). Does the character have all sequences included in its animation?

Brent

Hello Brent,

Sure the character has all sequences included in its animation…Here is the sequence module.

https://hastebin.com/fuvudijoko.pl

I’m not familiar with that module, but at a quick look it appears OK.

This is probably an issue with your global variables and how they’re named or referenced. You’re stringing together quite a few things to create sequence names, for example (it appears) a sequence might be “Firstrun1”. Is that what you expect? Does your module specify the exact same sequence name as a possibility for that character?

Generally, global variables are bad. You should avoid them whenever possible. This tutorial helps explain why, and how:

https://docs.coronalabs.com/tutorial/basics/globals/index.html

You mention that you have multiple characters? Then globals are especially bad if you’re trying to use the same global variable like PLAYER_RUN for all of them.

Brent

yes…Am trying to use the same global variable  like  “PLAYER_RUN” and its siblings for all the characters animation sequence.

say I have 4 characters in which a single player module creates one player character during game play and changes it during a collision.

I tried putting  this in the collision listener to effect the change but still not working:

PLAYER_RUN = character[playerNum] … runSequence

PLAYER_JUMP = character[playerNum] … jumpSequence

PLAYER_DIE  = character[playerNum] … dieSequence

PLAYER_SHOOT = character[playerNum] … shootSequence

 

However the global variable “playerNum” do change but not the character…

 

I know global variables are now illegal but just wanted to make sure its works before converting it to a table and then require it where needed.

 

Uche.

It’s not that global variables are “illegal”, but they do tend to cause problems, so I always advise people to avoid them.

“self:setSequence()” is the correct command to change a sprite’s sequence, but it’s possible that PLAYER_RUN in that call is not a valid sequence at the exact time you call it there.

Here is where more “print()” statements will probably save you… try print()-ing out various values all throughout that collision handling function… the values of “event”, “event.phase”, “event.other.myType”, “event.target” and so forth. Make sure your logic adds up properly, and is what you expect. It might be a simple case of a variable being “nil” when you don’t expect it, and that’s terminating the logic flow.

Brent

Yeah…I have used print() to check for errors still did not find the problem…truly a road block.

Uche.

No errors/warning shown in the console? It should warn you if a requested sequence isn’t found and that it’s then defaulting to the first sequence, or something along those lines.

no errors/warning in the console…wish there was, at least it would point to the problem and the solution.

Is this  condition really working as expected?

 if( event.target and event.other.myType == "stone" )then 

because in local collision, like this seems to be, there is no “event.target” but “self” object.

“event.target” is refered in global collisions.

If this is a local collision, the condition never becomes true.

FYI, “event.target” exists for local collisions, and it’s the same as “self”. This chart illustrates the difference:

https://docs.coronalabs.com/api/event/collision/index.html#object-references

I think the issue for @uche is still the global variables and some confusion that’s occurring as a result.

Brent

@Brent

Your are right, of course. I checked for “event.target” (is a table) instead of “event.target.name” in my code, and the fun part is: i did it right using “self.name”.

Sorry for the misleading post.

Hi @uche,

It’s probably not the collision code but instead how you set up the sprite(s). Does the character have all sequences included in its animation?

Brent

Hello Brent,

Sure the character has all sequences included in its animation…Here is the sequence module.

https://hastebin.com/fuvudijoko.pl

I’m not familiar with that module, but at a quick look it appears OK.

This is probably an issue with your global variables and how they’re named or referenced. You’re stringing together quite a few things to create sequence names, for example (it appears) a sequence might be “Firstrun1”. Is that what you expect? Does your module specify the exact same sequence name as a possibility for that character?

Generally, global variables are bad. You should avoid them whenever possible. This tutorial helps explain why, and how:

https://docs.coronalabs.com/tutorial/basics/globals/index.html

You mention that you have multiple characters? Then globals are especially bad if you’re trying to use the same global variable like PLAYER_RUN for all of them.

Brent

yes…Am trying to use the same global variable  like  “PLAYER_RUN” and its siblings for all the characters animation sequence.

say I have 4 characters in which a single player module creates one player character during game play and changes it during a collision.

I tried putting  this in the collision listener to effect the change but still not working:

PLAYER_RUN = character[playerNum] … runSequence

PLAYER_JUMP = character[playerNum] … jumpSequence

PLAYER_DIE  = character[playerNum] … dieSequence

PLAYER_SHOOT = character[playerNum] … shootSequence

 

However the global variable “playerNum” do change but not the character…

 

I know global variables are now illegal but just wanted to make sure its works before converting it to a table and then require it where needed.

 

Uche.

It’s not that global variables are “illegal”, but they do tend to cause problems, so I always advise people to avoid them.

“self:setSequence()” is the correct command to change a sprite’s sequence, but it’s possible that PLAYER_RUN in that call is not a valid sequence at the exact time you call it there.

Here is where more “print()” statements will probably save you… try print()-ing out various values all throughout that collision handling function… the values of “event”, “event.phase”, “event.other.myType”, “event.target” and so forth. Make sure your logic adds up properly, and is what you expect. It might be a simple case of a variable being “nil” when you don’t expect it, and that’s terminating the logic flow.

Brent

Yeah…I have used print() to check for errors still did not find the problem…truly a road block.

Uche.

No errors/warning shown in the console? It should warn you if a requested sequence isn’t found and that it’s then defaulting to the first sequence, or something along those lines.

no errors/warning in the console…wish there was, at least it would point to the problem and the solution.