Author Topic: switch state problem  (Read 2749 times)

mightiest_hero

  • Member
  • **
  • Posts: 51
  • Karma: +0/-0
    • View Profile
switch state problem
« on: Fri, Sep 9, 2011 »
i use registry class like photonstorm suggest but i have a problem.
player is static public in registry extends FlxSprite
When i switch state from menu to playstate i run player = new Player()
and i add player to playstate it works
then when i switch state to playstate again with different level to load it throw error
if i don't add player = new Player() before add player
« Last Edit: Fri, Sep 9, 2011 by mightiest_hero »

camasthecat

  • Contributor
  • ****
  • Posts: 461
  • Karma: +0/-0
  • WOW! 400+ Posts!? Jeez! Do I have a life!?!?
    • View Profile
    • My site for thermalJS, an HTML5/Javascript engine kinda-like flixel:
Re: switch state problem
« Reply #1 on: Fri, Sep 9, 2011 »
OH!
Did you do this:
            public var player:Player;

And did you do the x and y coordinates?
like so:
   player = new Player(12,12);

mightiest_hero

  • Member
  • **
  • Posts: 51
  • Karma: +0/-0
    • View Profile
Re: switch state problem
« Reply #2 on: Fri, Sep 9, 2011 »
sorry wrong question please check first post i already edit it
« Last Edit: Fri, Sep 9, 2011 by mightiest_hero »

camasthecat

  • Contributor
  • ****
  • Posts: 461
  • Karma: +0/-0
  • WOW! 400+ Posts!? Jeez! Do I have a life!?!?
    • View Profile
    • My site for thermalJS, an HTML5/Javascript engine kinda-like flixel:
Re: switch state problem
« Reply #3 on: Fri, Sep 9, 2011 »
I don't really understand the question. Could you please rephrase the question?

mightiest_hero

  • Member
  • **
  • Posts: 51
  • Karma: +0/-0
    • View Profile
Re: switch state problem
« Reply #4 on: Fri, Sep 9, 2011 »
ok i already rephrase the question

Cowbacca

  • Active Member
  • ***
  • Posts: 107
  • Karma: +0/-0
    • View Profile
Re: switch state problem
« Reply #5 on: Sat, Sep 10, 2011 »
Try doing this in your registry instead if you wish to keep the same player object through the game:

public static var player:Player = new Player();

mightiest_hero

  • Member
  • **
  • Posts: 51
  • Karma: +0/-0
    • View Profile
Re: switch state problem
« Reply #6 on: Sat, Sep 10, 2011 »
yeah it is exactly what i do. but when i switch to playstate again throw error
if i don't add player:Player = new Player(); again making player new again


Arkeus

  • Contributor
  • ****
  • Posts: 321
  • Karma: +1/-0
    • View Profile
    • I, Arkeus
Re: switch state problem
« Reply #7 on: Sun, Sep 11, 2011 »
yeah it is exactly what i do. but when i switch to playstate again throw error
if i don't add player:Player = new Player(); again making player new again

In the new flixel, when you change states, it calls destroy on everything in the state. So if you add the player to the state, then switch states, it calls destroy on the state, which calls destroy on everything in the state including your player. The easiest way I found around this is that anything you have static that you add to states, override the destroy method in that class to do nothing.

mightiest_hero

  • Member
  • **
  • Posts: 51
  • Karma: +0/-0
    • View Profile
Re: switch state problem
« Reply #8 on: Sun, Sep 11, 2011 »
thanks for your info Arkeus

Lnub

  • Member
  • **
  • Posts: 13
  • Karma: +0/-0
    • View Profile
Re: switch state problem
« Reply #9 on: Sun, Sep 11, 2011 »
I too was just having a null object reference error whenever I attempted to reset my play state. (If you're wondering, it was happening when the groups called add() upon the game's restarting)

To solve it, I made createGroups() and destroyGroups() public static functions in my Registry. I call create at the start of the game, and destroy when I go to reset, and it works.

mightiest_hero

  • Member
  • **
  • Posts: 51
  • Karma: +0/-0
    • View Profile
Re: switch state problem
« Reply #10 on: Sun, Sep 11, 2011 »
thanks for the solution Lnub

relinwar

  • Member
  • **
  • Posts: 17
  • Karma: +0/-0
    • View Profile
Re: switch state problem
« Reply #11 on: Mon, Sep 19, 2011 »
The override destroy function works and I'm curious about Lnub's suggestion. so..
To solve it, I made createGroups() and destroyGroups() public static functions in my Registry. I call create at the start of the game, and destroy when I go to reset, and it works.

And what does createGroups() and destroyGroups() do inside the function? My actionscript is still not very good.

Alextfish

  • Active Member
  • ***
  • Posts: 174
  • Karma: +0/-0
    • View Profile
Re: switch state problem
« Reply #12 on: Mon, Sep 19, 2011 »
In the new flixel, when you change states, it calls destroy on everything in the state. So if you add the player to the state, then switch states, it calls destroy on the state, which calls destroy on everything in the state including your player. The easiest way I found around this is that anything you have static that you add to states, override the destroy method in that class to do nothing.
Yowch! I don't think I'd have guessed that. Thanks for the info.