Copy link to clipboard
Copied
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 do
...Copy link to clipboard
Copied
What code have you tried so far? What type of random motion... motion can happen in very small increments so how random and how sustained should motion be? What is involved with "exchange of sprites in every direction"... what does that mean?
Copy link to clipboard
Copied
For example, the sprite of the enemy would be walking through the scene, and when walking on the right, would assume the sprite face to the right when walking up the sprite face up, and the diagonals
I tried to use this example but I could not do what he wanted.
http://www.freeactionscript.com/page/4/
(Enemy AI - random movement)
Thanks for the feedback
Copy link to clipboard
Copied
That doesn't do much to answer the questions and explain your intentions. Try looking thru the following search thru Google for a tutorial to get you started...
https://www.google.com/search?q=AS3+random+motion+tutorial
Don't focus on getting your solution... focus on gaining an understanding of how to control movement in different ways.
Copy link to clipboard
Copied
Look this image, I would have this result, the tutorials I've seen, only vectors are shown with no prospect of direction.
.
Copy link to clipboard
Copied
This code should work fine for you. All you need to do is add this to the project's class and give the 4 sprites on your library the following linkage names:
if x++ && y++ Linkage name: "Sprite1";
if x-- && y-- Linkage name: "Sprite2";
if x ++ && y -- Linkage name: "Sprite3";
if x -- && y ++ Linkage name: "Sprite4";
I could make the character move much more smoother if you want... But with this code you get an idea.
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();
}
Copy link to clipboard
Copied
Very nice bro.
But to use this code have to create a separate .as file, and call it as a class?
Thanks for the reply
Copy link to clipboard
Copied
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();
}
}
}
Copy link to clipboard
Copied
Awesome!
Got it, on that basis I will try to add a few more arrays as in the states Idle and attack.
That was what I needed.
Thanks a lot bro!
Find more inspiration, events, and resources on the new Adobe Community
Explore Now