justjake
04-02-2010, 02:24 AM
Actionscript 3 in Flash
Hi, i'm having a few object animating problems in Flash here and was wandering if someone could help.
I have got a block of code here that should make a number of "car" objects to spawn and travel side to side across the stage at a random speed and I am having some issues.
In this set of code things are almost working perfectly in terms of the animation. The cars go in one direction correctly, because their xspeed isn't being re-determined every enterFrame event. Whereas when they go the other direction, their xspeed is determined every enterFrame event so they move very jerkily.
var addedCar:Array = [false,false,false];
var spawnTimer:Timer = new Timer(10000, 3);
var cars:Array = [];
var rightspeed:Array = [];
var leftspeed:Array = [];
var carsLeft:Array = [false,false,false];
var carsRight:Array = [false,false,false];
var myCar1:car = new car();
cars.push(myCar1);
var myCar2:car = new car();
cars.push(myCar2);
var myCar3:car = new car();
cars.push(myCar3);
addChild(cars[0]);
addChild(cars[1]);
addChild(cars[2]);
function pressCheck(event:Event):void {
if (checkPress == true) {
if (playingGame == false) {
for (var i:int =0; i<3; i++) {
cars[i].visible = false;
}
}
if (playingGame == true) {
spawnTimer.start();
spawnTimer.addEventListener(TimerEvent.TIMER, zedSpawn0);
function zedSpawn0(event:TimerEvent):void {
if (addedCar[0] == true) {
carsLeft[0] = true;
}
addedCar[0] = true;
trace("car 0");
spawnTimer.stop();
spawnTimer.start();
spawnTimer.removeEventListener(TimerEvent.TIMER, zedSpawn0);
spawnTimer.addEventListener(TimerEvent.TIMER, zedSpawn1);
function zedSpawn1(event:TimerEvent):void {
if (addedCar[1] == true) {
carsLeft[1] = true;
}
addedCar[1] = true;
trace("car 1");
spawnTimer.stop();
spawnTimer.start();
spawnTimer.removeEventListener(TimerEvent.TIMER, zedSpawn1);
spawnTimer.addEventListener(TimerEvent.TIMER, zedSpawn2);
function zedSpawn2(event:TimerEvent):void {
if (addedCar[2] == true) {
carsLeft[2] = true;
}
addedCar[2] = true;
trace("car 2");
spawnTimer.stop();
spawnTimer.addEventListener(TimerEvent.TIMER, zedSpawn2);
}
}
}
for (var j:int =0; j<3; j++) {
cars[j].x=stage.width;
cars[j].y=325;
cars[j].visible = true;
cars[j].scaleX = 0.3;
cars[j].scaleY = 0.3;
}
}
}
}
}
function animation(event:Event):void {
if (playingGame == true) {
for (var k:int =0; k<3; k++) {
if (carsLeft[k] == true && addedCar[k] == true) {
cars[k].x+= Math.round(Math.random()*(-30))+0;
cars[k].scaleX = 0.3;
if (cars[k].x <= 0 - cars[k].width && addedCar[k] == true) {
rightspeed[k] = Math.round(Math.random()*(30))+0;
cars[k].x = 1;
carsLeft[k] = false;
carsRight[k] = true;
}
}
}
for (var l:int =0; l<3; l++) {
if (carsRight[l] == true && addedCar[k] == true) {
cars[l].x+= rightspeed[l];
cars[l].scaleX = -0.3;
}
}
for (var q:int =0; q<3; q++) {
if (cars[q].x >= 550 + cars[q].width && addedCar[q] == true) {
rightspeed[k] = Math.round(Math.random()*(30))+0;
carsRight[q] = false;
carsLeft[q] = true;
}
}
for (var m:int =0; m<3; m++) {
if (myFoot.hitTestObject(cars[m])) {
cars[m].visible = false;
//addedCar[m] = false;
addChild(cars[m]);
cars[m].x=stage.width;
cars[m].y=325;
cars[m].visible = true;
cars[m].scaleX = -0.3;
cars[m].scaleY = 0.3;
carsRight[m] = false;
carsLeft[m] = true;
}
}
}
}
I have a modified copy of this work where I applied the code that worked to determine a random speed for one direction, to both directions for my cars. With this code both direction speeds should never be determined every enterFrame event, like I said because that would make the animation jerky. But now that I have made these simple adjustments, the cars only spawn stuck to the right hand side of the stage, doing almost nothing properly. I am a bit baffled as I only seem to remember editing the speed at which the cars travel across the stage.
var playingGame:Boolean = false;
var addedCar:Array = [false,false,false];
var spawnTimer:Timer = new Timer(10000, 3);
var cars:Array = [];
var rightspeed:Array = [];
var leftspeed:Array = [];
var carsLeft:Array = [false,false,false];
var carsRight:Array = [false,false,false];
var beginGame:begin = new begin();
var xPos:Number = stage.width /2;
var yPos:Number = 400 - myRoad.height;
var myCar1:car = new car();
cars.push(myCar1);
var myCar2:car = new car();
cars.push(myCar2);
var myCar3:car = new car();
cars.push(myCar3);
addChild(cars[0]);
addChild(cars[1]);
addChild(cars[2]);
function pressCheck(event:Event):void {
if (checkPress == true) {
if (playingGame == false) {
for (var i:int =0; i<3; i++) {
cars[i].visible = false;
}
}
if (playingGame == true) {
spawnTimer.start();
spawnTimer.addEventListener(TimerEvent.TIMER, zedSpawn0);
function zedSpawn0(event:TimerEvent):void {
if (addedCar[0] == true) {
carsLeft[0] = true;
}
addedCar[0] = true;
trace("car 0");
spawnTimer.stop();
spawnTimer.start();
spawnTimer.addEventListener(TimerEvent.TIMER, zedSpawn1);
function zedSpawn1(event:TimerEvent):void {
if (addedCar[1] == true) {
carsLeft[1] = true;
}
addedCar[1] = true;
trace("car 1");
spawnTimer.stop();
spawnTimer.start();
spawnTimer.addEventListener(TimerEvent.TIMER, zedSpawn2);
function zedSpawn2(event:TimerEvent):void {
if (addedCar[2] == true) {
carsLeft[2] = true;
}
addedCar[2] = true;
trace("car 2");
spawnTimer.stop();
}
}
}
for (var j:int =0; j<3; j++) {
cars[j].x=stage.width;
cars[j].y=325;
cars[j].visible = true;
cars[j].scaleX = 0.3;
cars[j].scaleY = 0.3;
}
}
}
}
}
function animation(event:Event):void {
if (playingGame == true) {
for (var k:int =0; k<3; k++) {
if (carsLeft[k] == true && addedCar[k] == true) {
cars[k].x+= leftspeed[k];
cars[k].scaleX = 0.3;
if (cars[k].x <= 0 - cars[k].width ) {
cars[k].x = 1;
carsLeft[k] = false;
carsRight[k] = true;
rightspeed[k] = Math.round(Math.random()*(30))+0;
}
}
}
for (var l:int =0; l<3; l++) {
if (carsRight[l] == true && addedCar[l] == true) {
cars[l].x+= rightspeed[l];
cars[l].scaleX = -0.3;
}
}
for (var q:int =0; q<3; q++) {
if (cars[q].x >= 550 + cars[q].width && addedCar[q] == true) {
carsRight[q] = false;
carsLeft[q] = true;
leftspeed[k] = Math.round(Math.random()*(-30))+0;
}
}
}
for (var m:int =0; m<3; m++) {
if (myFoot.hitTestObject(cars[m])) {
cars[m].visible = false;
addChild(cars[m]);
cars[m].x=stage.width;
cars[m].y=325;
cars[m].visible = true;
cars[m].scaleX = -0.3;
cars[m].scaleY = 0.3;
carsRight[m] = false;
carsLeft[m] = true;
}
}
}
}
I am a little bit worried about all of this code that I am posting seeing as it is for a University assignment that I am doing. I'm worried this might be considered taking someone else's code if you were to help me. From what i gather it seems like there should be little chance of this even having a chance of being looked for by moderators, if it gets moderated, but seeing as I'm basically giving away half of my assignment I do wander...
Anyway erm, sorry. I've posted the two versions of my code there and would really appreciate some help on my said problems. I didn't post all of the code obviously so there are bits referring to bits that aren't there, but i'm sure you know that. I just wanted to isolate the code that was the problem and all the related code that could effect it.
Thankyou
Hi, i'm having a few object animating problems in Flash here and was wandering if someone could help.
I have got a block of code here that should make a number of "car" objects to spawn and travel side to side across the stage at a random speed and I am having some issues.
In this set of code things are almost working perfectly in terms of the animation. The cars go in one direction correctly, because their xspeed isn't being re-determined every enterFrame event. Whereas when they go the other direction, their xspeed is determined every enterFrame event so they move very jerkily.
var addedCar:Array = [false,false,false];
var spawnTimer:Timer = new Timer(10000, 3);
var cars:Array = [];
var rightspeed:Array = [];
var leftspeed:Array = [];
var carsLeft:Array = [false,false,false];
var carsRight:Array = [false,false,false];
var myCar1:car = new car();
cars.push(myCar1);
var myCar2:car = new car();
cars.push(myCar2);
var myCar3:car = new car();
cars.push(myCar3);
addChild(cars[0]);
addChild(cars[1]);
addChild(cars[2]);
function pressCheck(event:Event):void {
if (checkPress == true) {
if (playingGame == false) {
for (var i:int =0; i<3; i++) {
cars[i].visible = false;
}
}
if (playingGame == true) {
spawnTimer.start();
spawnTimer.addEventListener(TimerEvent.TIMER, zedSpawn0);
function zedSpawn0(event:TimerEvent):void {
if (addedCar[0] == true) {
carsLeft[0] = true;
}
addedCar[0] = true;
trace("car 0");
spawnTimer.stop();
spawnTimer.start();
spawnTimer.removeEventListener(TimerEvent.TIMER, zedSpawn0);
spawnTimer.addEventListener(TimerEvent.TIMER, zedSpawn1);
function zedSpawn1(event:TimerEvent):void {
if (addedCar[1] == true) {
carsLeft[1] = true;
}
addedCar[1] = true;
trace("car 1");
spawnTimer.stop();
spawnTimer.start();
spawnTimer.removeEventListener(TimerEvent.TIMER, zedSpawn1);
spawnTimer.addEventListener(TimerEvent.TIMER, zedSpawn2);
function zedSpawn2(event:TimerEvent):void {
if (addedCar[2] == true) {
carsLeft[2] = true;
}
addedCar[2] = true;
trace("car 2");
spawnTimer.stop();
spawnTimer.addEventListener(TimerEvent.TIMER, zedSpawn2);
}
}
}
for (var j:int =0; j<3; j++) {
cars[j].x=stage.width;
cars[j].y=325;
cars[j].visible = true;
cars[j].scaleX = 0.3;
cars[j].scaleY = 0.3;
}
}
}
}
}
function animation(event:Event):void {
if (playingGame == true) {
for (var k:int =0; k<3; k++) {
if (carsLeft[k] == true && addedCar[k] == true) {
cars[k].x+= Math.round(Math.random()*(-30))+0;
cars[k].scaleX = 0.3;
if (cars[k].x <= 0 - cars[k].width && addedCar[k] == true) {
rightspeed[k] = Math.round(Math.random()*(30))+0;
cars[k].x = 1;
carsLeft[k] = false;
carsRight[k] = true;
}
}
}
for (var l:int =0; l<3; l++) {
if (carsRight[l] == true && addedCar[k] == true) {
cars[l].x+= rightspeed[l];
cars[l].scaleX = -0.3;
}
}
for (var q:int =0; q<3; q++) {
if (cars[q].x >= 550 + cars[q].width && addedCar[q] == true) {
rightspeed[k] = Math.round(Math.random()*(30))+0;
carsRight[q] = false;
carsLeft[q] = true;
}
}
for (var m:int =0; m<3; m++) {
if (myFoot.hitTestObject(cars[m])) {
cars[m].visible = false;
//addedCar[m] = false;
addChild(cars[m]);
cars[m].x=stage.width;
cars[m].y=325;
cars[m].visible = true;
cars[m].scaleX = -0.3;
cars[m].scaleY = 0.3;
carsRight[m] = false;
carsLeft[m] = true;
}
}
}
}
I have a modified copy of this work where I applied the code that worked to determine a random speed for one direction, to both directions for my cars. With this code both direction speeds should never be determined every enterFrame event, like I said because that would make the animation jerky. But now that I have made these simple adjustments, the cars only spawn stuck to the right hand side of the stage, doing almost nothing properly. I am a bit baffled as I only seem to remember editing the speed at which the cars travel across the stage.
var playingGame:Boolean = false;
var addedCar:Array = [false,false,false];
var spawnTimer:Timer = new Timer(10000, 3);
var cars:Array = [];
var rightspeed:Array = [];
var leftspeed:Array = [];
var carsLeft:Array = [false,false,false];
var carsRight:Array = [false,false,false];
var beginGame:begin = new begin();
var xPos:Number = stage.width /2;
var yPos:Number = 400 - myRoad.height;
var myCar1:car = new car();
cars.push(myCar1);
var myCar2:car = new car();
cars.push(myCar2);
var myCar3:car = new car();
cars.push(myCar3);
addChild(cars[0]);
addChild(cars[1]);
addChild(cars[2]);
function pressCheck(event:Event):void {
if (checkPress == true) {
if (playingGame == false) {
for (var i:int =0; i<3; i++) {
cars[i].visible = false;
}
}
if (playingGame == true) {
spawnTimer.start();
spawnTimer.addEventListener(TimerEvent.TIMER, zedSpawn0);
function zedSpawn0(event:TimerEvent):void {
if (addedCar[0] == true) {
carsLeft[0] = true;
}
addedCar[0] = true;
trace("car 0");
spawnTimer.stop();
spawnTimer.start();
spawnTimer.addEventListener(TimerEvent.TIMER, zedSpawn1);
function zedSpawn1(event:TimerEvent):void {
if (addedCar[1] == true) {
carsLeft[1] = true;
}
addedCar[1] = true;
trace("car 1");
spawnTimer.stop();
spawnTimer.start();
spawnTimer.addEventListener(TimerEvent.TIMER, zedSpawn2);
function zedSpawn2(event:TimerEvent):void {
if (addedCar[2] == true) {
carsLeft[2] = true;
}
addedCar[2] = true;
trace("car 2");
spawnTimer.stop();
}
}
}
for (var j:int =0; j<3; j++) {
cars[j].x=stage.width;
cars[j].y=325;
cars[j].visible = true;
cars[j].scaleX = 0.3;
cars[j].scaleY = 0.3;
}
}
}
}
}
function animation(event:Event):void {
if (playingGame == true) {
for (var k:int =0; k<3; k++) {
if (carsLeft[k] == true && addedCar[k] == true) {
cars[k].x+= leftspeed[k];
cars[k].scaleX = 0.3;
if (cars[k].x <= 0 - cars[k].width ) {
cars[k].x = 1;
carsLeft[k] = false;
carsRight[k] = true;
rightspeed[k] = Math.round(Math.random()*(30))+0;
}
}
}
for (var l:int =0; l<3; l++) {
if (carsRight[l] == true && addedCar[l] == true) {
cars[l].x+= rightspeed[l];
cars[l].scaleX = -0.3;
}
}
for (var q:int =0; q<3; q++) {
if (cars[q].x >= 550 + cars[q].width && addedCar[q] == true) {
carsRight[q] = false;
carsLeft[q] = true;
leftspeed[k] = Math.round(Math.random()*(-30))+0;
}
}
}
for (var m:int =0; m<3; m++) {
if (myFoot.hitTestObject(cars[m])) {
cars[m].visible = false;
addChild(cars[m]);
cars[m].x=stage.width;
cars[m].y=325;
cars[m].visible = true;
cars[m].scaleX = -0.3;
cars[m].scaleY = 0.3;
carsRight[m] = false;
carsLeft[m] = true;
}
}
}
}
I am a little bit worried about all of this code that I am posting seeing as it is for a University assignment that I am doing. I'm worried this might be considered taking someone else's code if you were to help me. From what i gather it seems like there should be little chance of this even having a chance of being looked for by moderators, if it gets moderated, but seeing as I'm basically giving away half of my assignment I do wander...
Anyway erm, sorry. I've posted the two versions of my code there and would really appreciate some help on my said problems. I didn't post all of the code obviously so there are bits referring to bits that aren't there, but i'm sure you know that. I just wanted to isolate the code that was the problem and all the related code that could effect it.
Thankyou