3
\$\begingroup\$

I'm trying to make a room full of monster each side and the player stay in the middle. I have a problem, I don't know how I move each monster to the player and rotate each of them to face the player, all object is clip (in the library)... Can someone help me.

here the code of the game (Game - in english):

package cem {

import flash.display.MovieClip;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.events.Event;  


public class Game extends MovieClip {
    private var _myPlayer: Player = new Player,                 
                _myTimer: Timer,
                _limitTime: uint = 1000,
                _containerMonster: MovieClip = new MovieClip;                   
    
    public function Jeu() {
        // constructor code
        addChild(_myPlayer);
        addChild(_containerMonster);
        
        _myPlayer.x = 500;
        _myPlayer.y = 450;
        
        
        _myTimer = new Timer(_limitTime);
        _myTimer.addEventListener(TimerEvent.TIMER, _createMonster);                    
        _myTimer.start();
    }
    
    private function _createMonster(e: TimerEvent){         
        var a:Monster = new Monster;            
        var b:Monster = new Monster;
        var c:Monster = new Monster;
        var d:Monster = new Monster;            
        var placeMonsterTop: Array = ["178", "302", "426", "550", "674", "798", "922", "1046"];
        var placeMonsterRight: Array = ["118", "248", "372", "496", "620"];
        var placeMonsterDown: Array = ["178", "302", "426", "550", "674", "798", "922", "1046"];
        var placeMonsterLeft: Array = ["118", "248", "372", "496", "620"];
        _containerMonster.addChild(a);
        _containerMonster.addChild(b);
        _containerMonster.addChild(c);
        _containerMonster.addChild(d);
        a.x = placeMonsterTop[Math.floor(Math.random() * placeMonsterTop.length)];
        a.y = 0;
        b.x = 1222;
        b.y = placeMonsterRight[Math.floor(Math.random() * placeMonsterRight.length)];
        c.x = placeMonsterDown[Math.floor(Math.random() * placeMonsterDown.length)];
        c.y = 760;
        d.x = -46;
        d.y = placeMonsterLeft[Math.floor(Math.random() * placeMonsterLeft.length)];        
}}}

My hierarchy is like: main folder - into the main folder I have the .fla and a folder name cem - into cem I have two .as files one name: Jeu.as an the other name Monstre.as.

I use OOP for my school.

In my cem/Monstre.as code I have nothing for the moment, but I would like to put the moving code in it.

Here the picture of my .swf (like the entire game)

The blue circle is my player and the blue squares are all my monsters. They pop each 1 sec. They pick a random value in the assignment array (like for the monster a they pick inn the top array...) and they pop. Now I only need to move it to the player, rotate it when they move or at the start (to face the player) and stop it when they reach the player.

\$\endgroup\$
0

1 Answer 1

1
\$\begingroup\$

So here is what I would do:

  1. In the Monster class constructor, you need to pass the player instance and store it, this way each monster will be able to know its target and chase it.
  2. Add an event receiver for enter frame event in Monster class
  3. In the enter frame function, you need to calculate the angle to the player similar to this post, and rotate the sprite to face the player.
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .