I have a button entity and 3 other entities added to the world. Each (non-button) entity’s graphic property has an Image object assigned to it which contains an embedded PNG. Clicking the button will change what each entity is displaying, randomly choosing a new embedded PNG. The only way I know how to do this is to initialize the Image objects (so I can pass a different PNG to the constructor) every time the button is clicked. I’m noticing through the console that MEM: keeps increasing every time the button is clicked. Is there a way to change the bitmap in an Image object, instead of re-initializing, or am I just going about this wrong?
package entities
{
import entities.EntityPlus;
import net.flashpunk.FP;
import net.flashpunk.Graphic;
import net.flashpunk.graphics.Image;
import net.flashpunk.Mask;
import constants.ImageConstants;
import entities.Word1;
import entities.Word2;
import entities.Word3;
/**
* ...
* @author Jacob Stewart
*/
public class GeneratorButton extends Button
{
private var normalImg:Image = new Image(ImageConstants.GENERATORBTNNORMAL);
private var hoverImg:Image = new Image(ImageConstants.GENERATORBTNHOVER);
private var word1Img:Image;
private var word1:EntityPlus;
private var word2Img:Image;
private var word2:EntityPlus;
private var word3Img:Image;
private var word3:EntityPlus;
public function GeneratorButton(x:Number=0, y:Number=0, graphic:Graphic=null, mask:Mask=null)
{
super(x, y, graphic, mask);
}
override public function added():void
{
super.added();
word1 = new EntityPlus(0, 250);
word2 = new EntityPlus(0, 250);
word3 = new EntityPlus(0, 250);
world.add(word1);
world.add(word2);
world.add(word3);
generateInsult();
}
// Called when the button it clicked
override public function click():void
{
super.click();
generateInsult();
}
private function generateInsult():void
{
// imgConsts contains arrays of imbedded PNGs
word1Img = new Image(imgConsts.column1[FP.rand(imgConsts.column1.length)]);
word1.graphic = word1Img;
word2Img = new Image(imgConsts.column2[FP.rand(imgConsts.column2.length)]);
word2.graphic = word2Img;
word3Img = new Image(imgConsts.column3[FP.rand(imgConsts.column3.length)]);
word3.graphic = word3Img;
word1.x = 400 - ((word1Img.width + word2Img.width + word3Img.width + 60) * .5);
word2.x = word1.x + word1Img.width + 30;
word3.x = word2.x + word2Img.width + 30;
}
}
}