A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: AS2 - HitTest a Variable

  1. #1
    Junior Member
    Join Date
    Jan 2010
    Posts
    2

    AS2 - HitTest a Variable

    Hi all,

    I'm having problems hittesting a variable. The function fire is activated with space. This starts my character shooting. I want to remove the bullet variable when it hittests whatever i choose. At the moment this is "box".

    Presently, the hittest occurs every time space is pressed and not when the bullet hits the box. Based on that i'm thinking that it's where i'm placing the hittest and not the hittest code.

    I've tried placing it in various parts of my entire code but then i don't get a hittest at all.

    Can anybody help please? I'd really appreciate some advice.

    thanks in advance

    Code:
    function fire(){
        if(reloaded){
            x=maze.getNextHighestDepth();
            var bullet:MovieClip=maze.attachMovie("shot", "shot"+x, x);
            bullet._x=_root.crosshair._x-maze._x;
            bullet._y=_root.crosshair._y-maze._y;
            randomNum=random(bulletOffset)-bulletOffset/2;
            bullet._rotation=crosshair._rotation+randomNum;
            bullet.xSpeed=Math.cos(Math.PI/180 * bullet._rotation)*bulletSpeed;
            bullet.ySpeed=Math.sin(Math.PI/180 * bullet._rotation)*bulletSpeed;
            bullet.life=0;
            for(i=x;i<(x+1);i++){
                    if("shot"+i.hitTest(box)){
                        trace ("x ="+(x));
                        trace ("i ="+(i));
                        trace (bullet);
                        }
                    }
            
            bullet.onEnterFrame=function(){
                
                this._x+=this.xSpeed;
                this._y+=this.ySpeed;
                this.life++;
                
                if(this.life>totalLife){
                    this.removeMovieClip();
                    this.unloadMovie();
                    }            
                    
                
                    
            }
    
            reload();
        };
    };
    
    function reload(){
        reloaded=false;
        timer=setInterval(this, "Reloaded", reloadSpeed);
    }
    function Reloaded(){
        clearInterval(timer);
        reloaded=true
    }

  2. #2
    Junior Member
    Join Date
    Jan 2010
    Posts
    2
    Hi,
    It helps if you follow part 2 of a tutorial!!

    I adapted the code to suit my own needs.



    function fire(){
    if(reloaded){
    x=maze.getNextHighestDepth();
    bullet_array.push("shot"+x);
    var bullet:MovieClip=maze.attachMovie("shot", "shot"+x, x);
    bullet._x=_root.crosshair._x-maze._x;
    bullet._y=_root.crosshair._y-maze._y;
    randomNum=random(bulletOffset)-bulletOffset/2;
    bullet._rotation=crosshair._rotation+randomNum;
    bullet.xSpeed=Math.cos(Math.PI/180 * bullet._rotation)*bulletSpeed;
    bullet.ySpeed=Math.sin(Math.PI/180 * bullet._rotation)*bulletSpeed;
    bullet.life=0;



    bullet.onEnterFrame=function(){

    this._x+=this.xSpeed;
    this._y+=this.ySpeed;
    this.life++;

    if(this.life>totalLife){
    this.removeMovieClip();
    this.unloadMovie();
    }


    };

    reload();
    }
    }

    function reload(){
    reloaded=false;
    timer=setInterval(this, "Reloaded", reloadSpeed);
    }
    function Reloaded(){
    clearInterval(timer);
    reloaded=true
    }
    for (x=0; x<2; x++) {
    attachEnemy();
    }
    function attachEnemy() {
    x = maze.getNextHighestDepth();
    var enemy:MovieClip = maze.attachMovie("box", "box"+x, x);
    enemy._x = 0;
    enemy._y = 0;
    enemy.onEnterFrame = function() {
    for (x in bullet_array) {
    if (this.hitTest(maze[bullet_array[x]])) {
    maze[bullet_array[x]].removeMovieClip();
    this.removeMovieClip();
    bullet_array.splice(x,1);
    _root.explosion(this._x,this._y);
    }
    }
    };
    }
    You can follow these tutorials here: http://www.actionscript.org/resource.../Andy-Crockett

    I would be interested to know any ideas on how i can implement this theory to multiple enemies with different characteristics such as some enemies requiring more hits to kill them?

    Thanks.

  3. #3
    Junior Member
    Join Date
    Jan 2010
    Posts
    5
    When you use
    Actionscript Code:
    "shot"+i.hitTest()

    you're not really accessing the "shot" MC's at all. Try:

    Actionscript Code:
    this["shot"+i].hitTest()

    To lookup a property of a symbol with AS2 you need to use the array syntax ( "[]" ) described above. This finds an object based on its instance name. With your code, Flash is actually looking for a method called hitTest() on a string value ("shot"+i) but of course there is none so it fails silently.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center