Best way to draw image without entitie


(Adam Edney) #1

I have a object which will have more than one sprite, inorder to draw it, (a Snake) so i want to loop through the snake a draw each body part, whats the best way of doing it? in the render section on the object.


(Jonathan Stoler) #2

If you’re using FlashPunk, you’ll need to use an Entity for this.

Something like:

public class Snake extends Entity
{

    private var size:uint = 1;

    public function eat():void
    {
        size++;
    }

    // etc etc etc

    override public function render():void
    {
        for(var i:uint = 0; i < size; i++){
            // draw snake part
        }

        super.render();
    }

}

Of course, your actual code will probably be significantly more complicated, depending on your needs, but this should provide a starting point for you.


(Adam Edney) #3

But the issue here is that all my snake parts are in a array and are only a x and y value so its not a Entity, so is there a way for one entity to draw more than on graphic?


(Jonathan Stoler) #4

Yes.

You can call addGraphic() for each of your graphics, or you can draw them directly using render() as I described above.

During its render cycle, your Entity will automatically draw each added Graphic.


(Adam Edney) #5

Thanks for the reply,

is there a way to add and remove selected graphics, for example, i have 3 body parts on my snake, is there a way to say body part one graphics .x = 10 and body part 3 graphic is removed? like a array


(Helios) #6

or you can draw them directly using render()

interesting, could you elaborate on that a bit? How do you draw additional graphics without calling addGraphic()? When you answered I thought you were implying that you repeatedly called addGraphic() in the forloop of the render function, which is a bit clunky. So how do you get one entity to contain a list of added graphics that get updated in render() without ever calling addGraphic()?


(Jonathan Stoler) #7

No, you definitely don’t want to call addGraphic() over and over if you want good performance!

If you’re just maintaining a list of Graphics that you’re drawing every render() without calling addGraphic(), then you’re basically doing what FlashPunk does anyway and it’s unnecessary. When you call addGraphic() on an Entity, it adds the Graphic to a list maintained by the Entity, which is rendered every time render() is called. Just use addGraphic() somewhere like an overriden added() method if this is the behavior you want.

However, if you want to simply draw something that changes every frame (like the snake example), you can use render() plus the Draw class for something like this (note: very simplified):

override public function render():void
{
	for(var i:uint = 0; i < parts.length; i++){
		Draw.rect(parts[i].x, parts[i].y, 20, 20);
	}

	// note: you might want to call this first, depending
	// on what order you want things drawn
	super.render();
}

From my experience, most of the time you won’t need to override render() - but when you do, it can become a really powerful tool to fine-tune what shows up on screen!

I’ll note that you can also use the Draw class on BitmapData by using Draw.setTarget(). You can then create an Image out of it and add it to your Entity with addGraphic(). This prevents the need to use Draw functions every frame! (Note that this wouldn’t make sense for the snake example above.)