Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How do I program a character with random motion in the scene, including diagonal movement and exchange of sprites every direction. (AS3).

Community Beginner ,
Sep 13, 2014 Sep 13, 2014

Please, anybody help me.

TOPICS
ActionScript
1.6K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Explorer , Sep 16, 2014 Sep 16, 2014

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

...
Translate
LEGEND ,
Sep 13, 2014 Sep 13, 2014

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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Sep 13, 2014 Sep 13, 2014

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 13, 2014 Sep 13, 2014

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Sep 14, 2014 Sep 14, 2014

Look this image, I would have this result, the tutorials I've seen, only vectors are shown with no prospect of direction.

.Untitled-2.png

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 15, 2014 Sep 15, 2014

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();

  }

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Sep 15, 2014 Sep 15, 2014

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 16, 2014 Sep 16, 2014

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();

  }

  }

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Sep 17, 2014 Sep 17, 2014
LATEST

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!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines