How do I program a character with random motion in the scene, including diagonal movement and exchange of sprites every direction. (AS3).
Please, anybody help me.
Please, anybody help me.
Were you expecting a code within a main timeline frame? This is a code which should be put within a document's class. That's the most recommended method.
For that to happen, create a new folder, fill it with the FLA file and create a new folder called "classes", where you must have a class file (.as). Call it "Main.as".
Then, open up your FLA file and, with nothing selected, open the properties window. In there, there is a Text Input Field which represents the Class. Write "classes.Main". After doing so, open the library window, where you should have your 4 sprites. Right click one of them and select Properties. A new window will pop up where you should check the "Export to ActionScript" option box. In the Class Text Input Field write "classes.Sprite1". In my previous reply I told you what should be the Sprite1, Sprite2, Sprite3 and Sprite4... Well, this is where you use that formula. In the second Text Input Field (which I don't know how it's written in the English version of the program... maybe Class Base?), write "flash.display.Sprite", and finally, click OK. Do the same thing with each of the other 3 Sprites.
Inside the "Main.as" class file, paste this code:
package classes {
import flash.display.MovieClip;
import flash.geom.Rectangle;
import flash.display.Sprite;
import flash.display.Graphics;
import flash.utils.Timer;
import flash.events.TimerEvent;
public class Main extends MovieClip {
public var Enemy: Sprite = new Sprite();
public var _Sprite1: Sprite1 = new Sprite1();
public var _Sprite2: Sprite2 = new Sprite2();
public var _Sprite3: Sprite3 = new Sprite3();
public var _Sprite4: Sprite4 = new Sprite4();
public var Sprites: Array = new Array(_Sprite1, _Sprite2, _Sprite3, _Sprite4);
public var Movements: Array = new Array("Right", "Left", "Up", "Down");
public var EnemyInicialPosition: Array = new Array(100, 100); // X, Y values
public var EnemySpeed:Number = 20;
public var MovementsDelay:int = 500; // Miliseconds
public var Delay:Timer = new Timer(MovementsDelay);
public function Main() {
CreateEnemy();
RandomMovements();
Delay.addEventListener(TimerEvent.TIMER, TimerFinished);
Delay.start();
}
public function CreateEnemy() {
Enemy.x = EnemyInicialPosition[0];
Enemy.y = EnemyInicialPosition[1];
stage.addChild(Enemy);
Enemy.addChild(Sprites[0]);
for (var i: int = 1; i < Sprites.length; i++) {
Sprites.visible = false;
Enemy.addChild(Sprites);
}
}
public function RandomMovements() {
var IndexNumber:int = Math.floor(Math.random() * (Movements.length));
var Direction: String = Movements[IndexNumber];
for (var i: int = 0; i < Sprites.length; i++) {
Sprites.visible = false;
}
if (Direction == "Right") {
Enemy.x += EnemySpeed;
Enemy.y += EnemySpeed;
Sprites[IndexNumber].visible = true;
}
if (Direction == "Left") {
Enemy.x -= EnemySpeed;
Enemy.y -= EnemySpeed;
Sprites[IndexNumber].visible = true;
}
if (Direction == "Up") {
Enemy.x += EnemySpeed;
Enemy.y -= EnemySpeed;
Sprites[IndexNumber].visible = true;
}
if (Direction == "Down") {
Enemy.x -= EnemySpeed;
Enemy.y += EnemySpeed;
Sprites[IndexNumber].visible = true;
}
}
private function TimerFinished (e:TimerEvent) {
RandomMovements();
}
}
}
Already have an account? Login
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.