Author Topic: Sprite problem  (Read 1974 times)

DeifiedExile

  • New Member
  • *
  • Posts: 2
  • Karma: +0/-0
    • View Profile
Sprite problem
« on: Fri, Jan 20, 2012 »
Hi everyone,
So I'm trying to learn AS3/flixel/Flash, and have been following a tutorial online(http://www.creativeapplications.net/flash/flixel-2-tutorial-flash-tutorials-games/)

unfortunately I've now run into a problem. When I press the key to make the sprite move, instead of redrawing the frame, it just draws the sprite in its new location on top of the old image. I'm sure its probably something really obvious, but I just can't seem to find the problem.
     I know I could write out the code to manually erase and then redraw the image, but, like I said, I'm new to the language(s)/engine and wasn't sure how that would affect other things in the future(collision, etc.)
Code is below, Image of my problem is attached
Code: [Select]
package
{
   import org.flixel.*;   
   import de.pixelate.flixelprimer.*;

    [SWF(width = "640", height = "480", backgroundColor = "0x008040")]
    [Frame(factoryClass="Preloader")]
   
   public class Main extends FlxGame
   {
     
      public function Main():void
      {
         super(640, 480, PlayState, 1);
         
      }
     
   }
   
}

Code: [Select]
package 
{

   
    import org.flixel.system.FlxPreloader;
   
   public class Preloader extends FlxPreloader
   {
     
      public function Preloader():void
      {
         className = "Main";
         super();
      }
     
   }

}

Code: [Select]
package de.pixelate.flixelprimer
{

   
    import org.flixel.*;
    import de.pixelate.flixelprimer.*;
   
   public class PlayState extends FlxState
   {
      private var _character: Character;
     
      override public function create():void
      {
         
         FlxG.bgColor = 0x008040;
         
         _character = new Character();
         add(_character);
         
         super.create();
      }
     
      override public function update():void
      {
         super.update();
      }
   }

}
Code: [Select]
package  de.pixelate.flixelprimer
{

   
    import org.flixel.*;
   
   public class Character extends FlxSprite
   {
      [Embed(source = "../../../../resources/images/Jet.png")]
      private var ImgCharacter:Class;
     
      public function Character():void
      {
         super(50, 50, ImgCharacter);
      }
     
      override public function update():void
      {
         velocity.x = 0;
         velocity.y = 0;
         
         if ((FlxG.keys.LEFT) || (FlxG.keys.A))
         {
            velocity.x = -200;
         }
         else if (FlxG.keys.RIGHT || FlxG.keys.D)
         {
            velocity.x = 200;
         }
         
         if (FlxG.keys.UP || FlxG.keys.W)
         {
            velocity.y = -200;
         }
         else if (FlxG.keys.DOWN || FlxG.keys.S)
         {
            velocity.y = 200;
         }
         
         super.update();
      }
     
   }

}

Thanks in advance for any help you guys can provide. I searched all over for answers but no luck. Sorry for the long post.

test84

  • Key Contributor
  • *****
  • Posts: 1328
  • Karma: +0/-0
  • ت
    • View Profile
    • My personal site.
Re: Sprite problem
« Reply #1 on: Fri, Jan 20, 2012 »
For starters, in your character class, move super.update to top of your update call. If that doesn't help, try giving it a small acceleration value, like 50 too.

If not, zip the whole project (including flixel and your project files, not just source files) and upload it to mediafire.com and post the link here or PM it to me.
blog, twitter, Check out my award winning game, Rot Gut:

KunoNoOni

  • "Never tell me the odds"
  • Active Member
  • ***
  • Posts: 170
  • Karma: +0/-0
  • Code is Love
    • View Profile
Re: Sprite problem
« Reply #2 on: Mon, Jan 23, 2012 »
I agree with test84, move the super.update().


-KunoNoOni

DeifiedExile

  • New Member
  • *
  • Posts: 2
  • Karma: +0/-0
    • View Profile
Re: Sprite problem
« Reply #3 on: Mon, Jan 23, 2012 »
Ok, so I moved the super.update to the top of the call, and reduced the velocity to 50, and I still get the repeating sprite images, they're just closer together now. I zipped the project and uploaded it as requested. Here is the link:

http://www.mediafire.com/?0atuu9mxoxsudqk

Thanks for responding so quickly and for whatever help you can give.

Foxtacy

  • Aspiring Indie Game Developer, Prototype Musician, Lame Pixel Artist
  • Member
  • **
  • Posts: 29
  • Karma: +0/-0
    • View Profile
    • Need songs? :3
Re: Sprite problem
« Reply #4 on: Mon, Jan 30, 2012 »
FOUND IT!

Check this out written on FlxState.as:

/**
* This function is called after the game engine successfully switches states.
* Override this function, NOT the constructor, to initialize or set up your game state.
* We do NOT recommend overriding the constructor, unless you want some crazy unpredictable things to happen!
*/

Instead of Overriding create. Whenever using states, you should do the same stuff you're doing on create, on your state's constructor.

like this:

public function PlayState()
{
         
        FlxG.bgColor = 0x008040;
         
   _character = new Character();
   add(_character);
         

}

Hope i have helped :D
Waiting is wasting

Foxtacy

  • Aspiring Indie Game Developer, Prototype Musician, Lame Pixel Artist
  • Member
  • **
  • Posts: 29
  • Karma: +0/-0
    • View Profile
    • Need songs? :3
Re: Sprite problem
« Reply #5 on: Mon, Jan 30, 2012 »
Also, i believe this kind of stuff you should ask on the HELP session, not here. This  makes mods happy =3 :P
Waiting is wasting