Author Topic: Bug when changing states  (Read 1510 times)

dredge

  • New Member
  • *
  • Posts: 4
  • Karma: +0/-0
    • View Profile
Bug when changing states
« on: Fri, Apr 22, 2011 »
Hi,

I have a big problem with the displaying of my cuts scene.
My game contains two main states :
- PlayState > The whole logic of the game
- DisplayLevelState > The screen who show, between levels, the number of the next level.

My "DisplaylevelState.as" is very simple, as you can see below. This class uses the dynamic variable "level" to display during 4s the next level !

Code: [Select]
import org.flixel.*;

public class DisplayLevelState extends FlxState
{
public var timer:Number = 4;
public var txt:FlxText;
public var level:String;

public function DisplayLevelState()
{
level = "LEVEL 1-"+FlxG.level;

txt = new FlxText(0, FlxG.height - 100, FlxG.width,level.toString())
FlxG.log(level);
txt.setFormat(null, 14, 0xFFFFFFFF, "center");
add(txt);
}

public override function update():void {
if (timer > 0) {
timer -= FlxG.elapsed;

if(timer <= 0){
FlxG.state = new PlayState();
}
}

}
}


The first time (before the first transit by PlayState), the display of the screen work perfectly. But after the end of the first level, when i call "FlxG.state = new DisplayLevelState();", I got a black screen (without the text)  during 4s !

Have I forgotten a important thing ?

Thanks in advance


cai

  • Contributor
  • ****
  • Posts: 465
  • Karma: +0/-0
  • the illest of villains
    • View Profile
    • Brandon Cash
Re: Bug when changing states
« Reply #1 on: Fri, Apr 22, 2011 »
I doubt this is related to changing states at all, but just that the text isn't getting displayed for some reason.  So, here's a few questions:
  • Does other text show up?  Try hitting `/~ and see if you can read things in the console.  If not, there is probably a problem loading the font.
  • If you add a FlxSprite or something, does it show up?  If not, there could be some reason why objects aren't getting displayed correctly.


Some things to consider:
  • You shouldn't have to get a String value of level with toString, since it's already a String.
  • Do you really need an extra state for this?  You could just have a fully opaque black rectangle over the screen with the text on it--then you could even do some kind of transition to the level behind it!
Follow me on Twitter | Come join us at #flixel on irc.freenode.net!

Arkeus

  • Contributor
  • ****
  • Posts: 321
  • Karma: +1/-0
    • View Profile
    • I, Arkeus
Re: Bug when changing states
« Reply #2 on: Sat, Apr 23, 2011 »
Add super.update() to the end of your update function.

Also you don't need the if(timer > 0) conditional.
« Last Edit: Sat, Apr 23, 2011 by Arkeus »

dredge

  • New Member
  • *
  • Posts: 4
  • Karma: +0/-0
    • View Profile
Re: Bug when changing states
« Reply #3 on: Tue, Apr 26, 2011 »
@Cai

I deleted the "toString()" which was of course useless.
I used FlxG.log to test my variables, and "level" contains always a text ! I confirm that I have the same problem in my state "GameOverState", made on the same model that "DisplayLevelState", where i use a string "Game over" in my FlxText.

Otherwise, I think that a extra state is useful because it enables me to clean the current "PlayState" to display the next level....

@Arkeus

I tried in vain your recommandations... :-(

@Everybody
You don't believe me, but then I call a player.reset() juste after my "FlxG.state = new DisplayLevelState();", the display of my cutscene state works perfectly !!

Has somebody have a logic explaination ?

Thanks in advance!

Arkeus

  • Contributor
  • ****
  • Posts: 321
  • Karma: +1/-0
    • View Profile
    • I, Arkeus
Re: Bug when changing states
« Reply #4 on: Tue, Apr 26, 2011 »
Make sure you added the super.update(), and then change the color parameter of the FlxText to be RRGGBB rather than AARRGGBB (ie, 0xffffff instead of 0xffffffff).

See if that works, if it doesn't, post all your code.

dredge

  • New Member
  • *
  • Posts: 4
  • Karma: +0/-0
    • View Profile
Re: Bug when changing states
« Reply #5 on: Thu, Apr 28, 2011 »
Unfortunately I have seen no change in spite of your recommandations.

Furthermore, I noticed that the texts of my cutscenes (between levels, gameover) don't never display itself at the same position on the screen. It's very strange and i don't understand why !!!!  :(

Please to find below my mains codes. I should greatful if someone would find a logic explaination !

PlayState.as
Code: [Select]
package
{
import org.flixel.*;

public class PlayState extends FlxState {
/* ...
code
... */


override public function create():void {
/* ...
code
... */

add(layerHUD);
addCoins();
addEnemies();
super.create();
}


override public function update():void {
player.collide(layerWorld);
enemies.collide(layerWorld);

FlxU.overlap(player, coins, overlapPlayerCoins);
if (!player.flickering()) FlxU.collide(player, enemies);

if (player.y > layerWorld.height) {
changeLifes(-1);
player.reset(24, 0);
}
if (player.x < 0) {
player.x = 0;
}else if ((player.x + player.width) > layerWorld.width) {
if (FlxG.level == 1){
FlxG.level = 2;
FlxG.state = new DisplayLevelState();
//player.reset(24, 0);
}else if (FlxG.level == 2){
FlxG.state = new GameOverState();
//player.reset(24, 0);
}

}

super.update();
camera.update();
}

protected function overlapPlayerCoins(player:FlxObject, coin:FlxObject):void
{
coin.kill();
FlxG.levels["coins"]++;
FlxG.play(SndCoin);


if (FlxG.levels["coins"] == 100){
changeLifes(-1);
FlxG.levels["coins"]=0;
}

layerHUDCoinsText.text = FlxG.levels["coins"].toString();


}


protected function addCoins():void
{
/* ...
code
... */
}

protected function addEnemies():void
{
/* ...
code
... */
}

public function increaseScore(_val:Number):void {
FlxG.score = FlxG.score+_val;
layerHUDScoreText.text = styliseScore(FlxG.score);
}

public function styliseScore(_val:Number):String {
var newScore:String = _val.toString();
if (_val<10) newScore="000000"+newScore;
else if (_val<100) newScore="00000"+newScore;
else if (_val<1000) newScore="0000"+newScore;
else if (_val<10000) newScore="000"+newScore;
else if (_val<100000) newScore="00"+newScore;
else if (_val<1000000) newScore="0"+newScore;

return newScore;
}

public function changeLifes(_val:Number):void {
FlxG.levels["lifes"] = FlxG.levels["lifes"] + _val;
layerHUDLifesText.text = FlxG.levels["lifes"].toString();

if (FlxG.levels["lifes"] == 0){
FlxG.state = new GameOverState();
//player.reset(24, 0);
}
}
}
}




DisplayLevelState.as
Code: [Select]
package
{
import org.flixel.*;

public class DisplayLevelState extends FlxState
{
public var timer:Number = 4;
public var leveltxt:FlxText;
public var level:String;

public function DisplayLevelState()
{
level = "LEVEL 1-"+FlxG.level;

leveltxt = new FlxText(0, 90, FlxG.width,level);
//leveltxt = new FlxText(0, 90, FlxG.width, "test");
leveltxt.setFormat(null, 14, 0xffffff, "center");
this.add(leveltxt);
}

public override function update():void {
timer -= FlxG.elapsed;
if(timer <= 0) FlxG.fade.start(0xff000000, 1, onFade);

super.update();
}

private function onFade():void
{

FlxG.state = new PlayState();
}
}
}


GameOverState.as
Code: [Select]
package
{
import org.flixel.*;

public class GameOverState extends FlxState
{
public var timer:Number = 5;
public var txtGameOver:FlxText;

public function GameOverState()
{
txtGameOver = new FlxText(0, 90, FlxG.width, "GAME OVER")
txtGameOver.setFormat(null, 14, 0xffffff, "center");
this.add(txtGameOver);
}

public override function update():void {
timer -= FlxG.elapsed;
if(timer <= 0) FlxG.fade.start(0xff000000, 1, onFade);

super.update();
}

private function onFade():void
{

FlxG.state = new MenuState();
}

}
}
« Last Edit: Thu, Apr 28, 2011 by dredge »

Adam Atomic

  • Founder
  • Key Contributor
  • *****
  • Posts: 852
  • Karma: +0/-0
  • new dad
    • View Profile
    • Adam Atomic
Re: Bug when changing states
« Reply #6 on: Thu, Apr 28, 2011 »
in your FlxStates, make sure you are overriding create() rather than the constructor!

dredge

  • New Member
  • *
  • Posts: 4
  • Karma: +0/-0
    • View Profile
Re: Bug when changing states
« Reply #7 on: Mon, May 2, 2011 »
Thanks Adam for your help, but I had always the same problem and I maybe understand now why

I corrected my cutscenes class following your recommandations

DisplayLevelState.as
Code: [Select]
package
{
import org.flixel.*;

public class DisplayLevelState extends FlxState
{
public var timer:Number = 4;
public var leveltxt:FlxText;
public var level:String;

override public function create():void
{
level = "LEVEL 1-"+FlxG.level;

leveltxt = new FlxText(0, 90, FlxG.width,level);
//leveltxt = new FlxText(0, 90, FlxG.width, "test");
leveltxt.setFormat(null, 14, 0xFFFFFFFF, "center");
this.add(leveltxt);

super.create();
}

override public function update():void {
timer -= FlxG.elapsed;
if(timer <= 0) FlxG.state = new PlayState();

super.update();
}
}
}

GameOverState.as
Code: [Select]
package
{
import org.flixel.*;

public class GameOverState extends FlxState
{
public var timer:Number = 5;
public var txtGameOver:FlxText;

override public function create():void
{
txtGameOver = new FlxText(0, 90, FlxG.width, "GAME OVER")
txtGameOver.setFormat(null, 14, 0xffffff, "center");
this.add(txtGameOver);

super.create();
}

override public function update():void {
timer -= FlxG.elapsed;
if(timer <= 0) FlxG.fade.start(0xff000000, 1, onFade);

super.update();
}

private function onFade():void
{

FlxG.state = new MenuState();
}

}
}

When my hero died at the beginning of the level(before the scroll of my level), the text is ill-placed. But if the hero died after, the cutscene moves on left and can be invisible. Do you have a solution to make the position of my cutscenes self-supported in comparison with my PlayState ?

Thanks in advance for your help