2
\$\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 (Jeu - in french):

package cem {

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


public class Jeu extends MovieClip {
    private var _monJoueur: Joueur = new Joueur,                    
                _horloge: Timer,
                _delai: uint = 1000,
                _conteneurMonstre: MovieClip = new MovieClip;                   

    public function Jeu() {
        // constructor code
        addChild(_monJoueur);
        addChild(_conteneurMonstre);

        _monJoueur.x = 500;
        _monJoueur.y = 450;


        _horloge = new Timer(_delai);
        _horloge.addEventListener(TimerEvent.TIMER, _creeMonstre);                  
        _horloge.start();
    }

    private function _creeMonstre(e: TimerEvent){           
        var a:Monstre = new Monstre;            
        var b:Monstre = new Monstre;
        var c:Monstre = new Monstre;
        var d:Monstre = new Monstre;            
        var emplacementMonstreHaut: Array = ["178", "302", "426", "550", "674", "798", "922", "1046"];
        var emplacementMonstreDroit: Array = ["118", "248", "372", "496", "620"];
        var emplacementMonstreBas: Array = ["178", "302", "426", "550", "674", "798", "922", "1046"];
        var emplacementMonstreGauche: Array = ["118", "248", "372", "496", "620"];
        _conteneurMonstre.addChild(a);
        _conteneurMonstre.addChild(b);
        _conteneurMonstre.addChild(c);
        _conteneurMonstre.addChild(d);
        a.x = emplacementMonstreHaut[Math.floor(Math.random() * emplacementMonstreHaut.length)];
        a.y = 0;
        b.x = 1222;
        b.y = emplacementMonstreDroit[Math.floor(Math.random() * emplacementMonstreDroit.length)];
        c.x = emplacementMonstreBas[Math.floor(Math.random() * emplacementMonstreBas.length)];
        c.y = 760;
        d.x = -46;
        d.y = emplacementMonstreGauche[Math.floor(Math.random() * emplacementMonstreGauche.length)];        
}}}

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 fils one name: Jeu.as an the other name Monstre.as

i code like OOP for my school.

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

Here the picture of my swf (like the entier game)

the blue cercle is my player and the blue square are all my monster. They pop each 1 sec. they pick a value random in the assignement 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) andstop it when they reach the player.

\$\endgroup\$
  • 1
    \$\begingroup\$ You'd have more chances to get an answer if your code was written in English. For a non-French user, this code looks like it's been obfuscated :P \$\endgroup\$ – Alexandre Vaillancourt Oct 4 '16 at 12:58
  • \$\begingroup\$ sorry i will but an english version of the code soon as possible \$\endgroup\$ – Hugo Chapdelaine Oct 4 '16 at 13:46
  • \$\begingroup\$ Is it still relevant to keep the French version in the question? \$\endgroup\$ – Alexandre Vaillancourt Oct 4 '16 at 14:28
0
\$\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\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.