//first make an array that stores 8 frames (a square rotating 90 degrees. I put each frame //on the stage just to show that by some miracle I got that part to work)
var currentRotation = 0
var frames = new Array()
var frameNum = 0
for (var i=0; i<8; i++){
var sq = new Sprite()
sq.graphics.beginFill(0x000000)
sq.graphics.drawRect(0,0,16,16)
sq.rotation += currentRotation
currentRotation += (90/8)
frames* = sq
stage.addChild(frames*)
frames*.x = i*50
}
//now I want to repeatedly loop through these 8 frames in 1 spot on the stage (ie. //animate the frames continuously). Doesn't work though.
stage.addEventListener(Event.ENTER_FRAME, playIt)
var container = new Sprite()
stage.addChild(container)
function playIt(e){
container.graphics.clear
container.addChild(frames[frameNum])
frameNum+=1
if (frameNum == 8){
frameNum = 0
}
}
I’m just experimenting doing simple things without getting errors.
Here, I merely want to visually animate 8 sprite frames continuously (happens to be a square rotating 90 degrees). Maybe a movieclip would be easier, but still as practice I want to use sprites.
On each ENTER_FRAME, I try to clear the previous frame and paste in the next frame, thus animating it. I think the problem might be that instead of ‘adding child’ each time I’m supposed to ‘paste’ it into container somehow instead.
I tried ‘copy motion as actionscript 3’ but it results in tons of weird non-as3-looking tags
This is my idea of making it work . I think there might be a more elegant way(probably using Bitmaps or sth like that; or turning strict compile mode off).
Note:
InstanceName.graphics.clear() doesn’t clear the graphics which belong to child DisplayObject’s so I use removeChild to remove the corresponding Sprite.
var currentRotation = 0
var frames = new Array()
var frameNum = 0;
for (var i=0; i<8; i++){
var sq = new Sprite()
sq.graphics.beginFill(0x000000)
sq.graphics.drawRect(0,0,16,16)
sq.rotation += currentRotation
currentRotation += (90/8)
frames* = sq
}
//now I want to repeatedly loop through these 8 frames in 1 spot on the stage (ie. //animate the frames continuously). Doesn't work though.
stage.addEventListener(Event.ENTER_FRAME, playIt)
var container = new Sprite();
stage.addChild(container);
function playIt(e){
if (frameNum >= 1) {
// remove the previous child (only if we are at frameNum >= 1, i.e. we have already addded a child)
container.removeChild(frames[frameNum-1]);
}
if (frameNum == 7){
frameNum = 0;
}
container.addChild(frames[frameNum]); // add a child
frameNum+=1;
}
Excellent, thanks. Simple enough that I probably should have figured it out myself but it takes forever when you don’t know what you’re doing.
My ultimate goal (one of them) is to RANDOMLY GENERATE animation similar to this gif:
(probably have to click the magnifying glass to make it bigger)
I don’t just mean draw the frames in flash and manipulate them, but actually have the program randomly generate new interesting behavior and draw stuff itself. Talk about a royal pain in the ***. At least I’ve taken a baby step onto mount everest.