Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411670 Posts in 69397 Topics- by 58452 Members - Latest Member: homina

May 16, 2024, 03:48:41 PM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Changing sprite on the fly
Pages: [1]
Print
Author Topic: Changing sprite on the fly  (Read 1084 times)
davidp
Level 6
*



View Profile WWW
« on: February 14, 2010, 03:44:27 AM »

So I've been working on this flash game again (learning to code in AS3 again) and now I'm trying to figure out how to change sprite on the fly.

I have sprite (.png) embedded in some class definition (for example player's class), but then i'd like to change that sprite to something else...

Point me in direction? Maybe some code snippet or some good example?

I've been trying to find something usefull around the internets, but normally end up with code blocks larger than my WHOLE game code (and i have background scrolling, some effects, enemies popping up, player movement, etc).
Logged

hatu
Level 2
**



View Profile
« Reply #1 on: February 14, 2010, 07:17:31 AM »

Can you post the whole class here?

Basically it's like this

removeChild(oldSprite);
addChild(newSprite);
Logged
davidp
Level 6
*



View Profile WWW
« Reply #2 on: February 14, 2010, 07:39:27 AM »

player class:
Code:
package  
{
import flash.display.Bitmap;

[Embed(source='../bin/player.png')]

public class PlayerClass extends Bitmap
{
public function PlayerClass()
{}
}
}
Logged

ChevyRay
Guest
« Reply #3 on: February 14, 2010, 08:26:27 AM »

Right. Realize that a "Sprite" in the AS3 library represents a graphical object that doubles as a container for more graphical objects. What you've created is a Bitmap, which is a class used for rendering BitmapData objects to the screen.

I suppose you could do something like this:

Code:
package  
{
import flash.display.Bitmap;
import flash.display.BitmapData;

public class PlayerClass extends Bitmap
{
[Embed(source='../bin/player.png')] private static var ImgPlayer:Class;
[Embed(source='../bin/player2.png')] private static var ImgPlayer2:Class;
[Embed(source='../bin/player3.png')] private static var ImgPlayer3:Class;

private static var playerImage:BitmapData = (new ImgPlayer).bitmapData;
private static var playerImage2:BitmapData = (new ImgPlayer2).bitmapData;
private static var playerImage3:BitmapData = (new ImgPlayer3).bitmapData;

public function PlayerClass()
{
// assign the bitmap image like this
bitmapData = playerImage;
}
}
}

Then you can assign the Bitmap object's bitmapData property to any of those three BitmapData objects on the fly. It's not the most convenient method if you want to display an animated bitmap, but it should work.

Sorry if that code is flawed at all, I was just about to head off to bed.
« Last Edit: February 14, 2010, 08:30:21 AM by ChevyRay » Logged
davidp
Level 6
*



View Profile WWW
« Reply #4 on: February 14, 2010, 08:32:26 AM »

ook, this looks like it could work Smiley tnx, will try it Smiley

edit: works like a charm Smiley thank a lot chevy!
« Last Edit: February 14, 2010, 01:53:28 PM by davidp » Logged

Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic