The forums have permanently moved to forum.kirupa.com. This forum will be kept around in read-only mode for archival purposes. To learn how to continue using your existing account on the new forums, check out this thread.

Page 1 of 2 12 LastLast
Results 1 to 15 of 23

Thread: Dynamically generated movieClipS hitTesting

  1. #1

    Fla Script Dynamically generated movieClipS hitTesting

    So I'm doing a little project that requires movie clips to be created through action script.

    Im not sure how to get them to hitTest because they are both created in seperate functions.

    PHP Code:
    //Enemy function
    newEnemy = function () {
        var 
    Enemy _root.attachMovie("angryEyes""angryEyes"+i_root.getNextHighestDepth());
        
    Enemy._y random(550);
        
    Enemy._x Stage.width;
        
    Enemy.onEnterFrame = function() {
            
    this._x -= random(20);
            
    this._y += -5+random(10);
            
    i++;
            if (
    wall.wall1.hitTest(this)) {
                
    removeMovieClip(this);
            }
            
    /*hitTest arrow with zombie!? 
            function shootZombie() {
                if (this.hitTest(angryEyes+i)) {
                    trace("hit");
                }
            }*/
        
    };
    };
    //Spawn new Enemy at an interval of 800ms
    setInterval(newEnemy800);
    //Function for shooting arrows and their direction 
    //(all things related to how they travel and are created)
    ShootBullet = function () {
        var 
    Bullet _root.attachMovie("Bullet""Bullet"+Num_root.getNextHighestDepth());
        var 
    point = {x:this.hero.gunTip._xy:this.hero.gunTip._y};
        
    this.hero.localToGlobal(point);
        
    Bullet._x point.x;
        
    Bullet._y point.y;
        
    Bullet._rotation _root.hero._rotation;
        
    Bullet.onEnterFrame = function() {
            
    Bullet._x += Math.cos(Bullet._rotation*(Math.PI/180))*35-5+random(10);
            
    Bullet._y += Math.sin(Bullet._rotation*(Math.PI/180))*35-5+random(10);
            
    //This is so that arrows will clear outside of the stage
            //It seems redundant, should use a switch case? or something
            
    if (wall.wall1.hitTest(Bullet) == true) {
                
    removeMovieClip(this);
            }
            if (
    wall.wall2.hitTest(Bullet) == true) {
                
    removeMovieClip(this);
            }
            if (
    wall.wall3.hitTest(Bullet) == true) {
                
    removeMovieClip(this);
            }
            if (
    wall.wall4.hitTest(Bullet) == true) {
                
    removeMovieClip(this);
            }
        };
        
    //unique identifier for each arrow
        
    Num++;
        
    //shows how many how many have been shot
        //trace(Num);
        
    shootZombie();
    }; 
    Last edited by t0w; April 19th, 2008 at 03:35 PM.

  2. #2
    I've never worked with AS2 but if u want to hittest a dynamically created movie clip then you'd want to store them in an array when u make them, then hittest all clips in that array.

    As far as accessing said array, make sure u create it outside of both functions.

  3. #3
    Okay just to clarify, both movie clips are dynamically created. Thanks for the tip on using arrays, I'll try to wrap my head around how to go about it. Further suggestions are welcome.

    ps. like my sig? just made it.

  4. #4
    Quote Originally Posted by t0w View Post
    ps. like my sig? just made it.
    It looks okay, but a mod might not approve of it

    http://www.kirupa.com/forumGuide/personalization.htm
    "If your footer does not adhere to the above guidelines, a moderator just may comment it out and place the lovable footer violation animation instead"
    2006-11 GlosRFC - Searching 8,168,684,336 brain cells

  5. Quote Originally Posted by glosrfc View Post
    It looks okay, but a mod might not approve of it

    http://www.kirupa.com/forumGuide/personalization.htm
    "If your footer does not adhere to the above guidelines, a moderator just may comment it out and place the lovable footer violation animation instead"
    Yea, the maximum size of a signature is soooo small here ><.

    To answer the question. If you want to hittest movieclips you'll need a reference to the movieclips involved, you can either do this by the path for example _root.angryEyes1337 or you use the variable the movieclip is, var enemy = _root.attach...

    But what you want to do is what glosrfc said, have 'm all in an array so you can access them easy for stuff like this. I already posted a very long post about this in an earlier topic.

    http://www.kirupa.com/forum/showthread.php?t=288440&
    Last edited by ArmoredSandwich; April 20th, 2008 at 10:01 AM. Reason: Added link

  6. #6
    Hey thanks, I'll be sure to give it a read. When I was trying by myself my idea was to add a listener for the x / y values of the bullet movie clips and put that into an array then cross reference the same idea for the targets. Didn't work too .. at all.

    Anyway, since this is all part of one project I guess I'll stick to posting in this topic.
    Can anyone possibly fathom what is going on here...
    (for loop not working? Seems correct but apparently not?) Would rather use for loop than 4 if statements.

    PHP Code:
            //Clear Arrows so they don't fly forever using for loop (because 4 walls)
            
    if (wall.wall1.hitTest(Bullet)) {
                
    removeMovieClip(this);
            }
            if (
    wall.wall2.hitTest(Bullet)) {
                
    removeMovieClip(this);
            }
            if (
    wall.wall3.hitTest(Bullet)) {
                
    removeMovieClip(this);
            }
            if (
    wall.wall4.hitTest(Bullet)) {
                
    removeMovieClip(this);
            }
            
    //This loop does the exact same as the 4 if statements above, except its not working? WTF!   
            
    for (var i:Number 1i<=4i++) {
                if (
    wall.wall[i].hitTest(Bullet)) {
                    
    removeMovieClip(this);
                }
            } 
    ps. will fix sig asap.
    Last edited by t0w; April 20th, 2008 at 04:04 PM.

  7. #7
    Okay now something REALLY weird is going on with arrays.

    First i'll quicky show the code I used instead of a for loop from the above post.
    PHP Code:
    if (wall.wall1.hitTest(Bullet) || wall.wall2.hitTest(Bullet) || wall.wall3.hitTest(Bullet) || wall.wall4.hitTest(Bullet)) {
                
    removeMovieClip(this);
            } 
    I took your advice and started learning about arrays.
    PHP Code:
    //Enemy Array
    var enemyArray = new Array();
    //Enemy function
    newEnemy = function () {
        var 
    Enemy _root.attachMovie("angryEyes""angryEyes"+i_root.getNextHighestDepth());
        
    Enemy._y random(550);
        
    Enemy._x 800;
        
    trace(enemyArray.pop());//taking the last value in array and outputting it
        
    Enemy.onEnterFrame = function() {
            
    this._x -= random(10);
            
    this._y += -2.5+random(5);
            if (
    wall.wall1.hitTest(this)) {
                
    removeMovieClip(this);
            }
        };
        
    i++;
        
    addEnemy enemyArray.push("zombie"+i);
    };
    //Spawn new Enemy at an interval of 800ms
    setInterval(newEnemy1000); 
    To explain what's happening best, a snippet from the output buffer:

    undefined
    zombie1
    zombie2
    zombie3
    zombie4
    zombie5


    Instead of giving me a Zombie[0] it gives me Undefined, does anyone have any idea why that is? Thanks again for all the help so far.
    Last edited by t0w; April 21st, 2008 at 04:35 PM.

  8. #8
    150
    posts
    Registered User
    add enemy to array before increment counter
    Everything is possible....

  9. #9
    Omg, thank you. I guess sometimes its the simplest explanation right, hehe.

    PHP Code:
    //Enemy Array
    var enemyArray = new Array();
    //Enemy function
    newEnemy = function () {
        var 
    Enemy _root.attachMovie("angryEyes""angryEyes"+i_root.getNextHighestDepth());
        
    Enemy._y random(550);
        
    Enemy._x 800;
        
    Enemy.onEnterFrame = function() {
            
    this._x -= random(10);
            
    this._y += -2.5+random(5);
            if (
    wall.wall1.hitTest(this)) {
                
    removeMovieClip(this);
            }
        };
        
    addEnemy enemyArray.push("zombie"+i);
        
    i++;
        
    trace(enemyArray.pop());
    };
    //Spawn new Enemy at an interval of 800ms
    setInterval(newEnemy1000); 
    That worked perfectly, thanks again.

  10. #10
    Sorry to double post again. But I am just wondering, is the enemyArray actually storing movie clips or just a string called "zombie"+i?

    I tried using .push(Enemy) but it seems like that is incorrect.
    Code:
    var Enemy = _root.attachMovie("angryEyes", "zombie"+i, _root.getNextHighestDepth());
        addEnemy = enemyArray.push(Enemy);

  11. #11
    150
    posts
    Registered User
    attachMovie returns MovieClip, than Enemy is MovieClip not a String.
    Everything is possible....

  12. #12
    Quote Originally Posted by Kosko View Post
    attachMovie returns MovieClip, than Enemy is MovieClip not a String.
    I'm not sure I follow...
    Code:
    var Enemy = _root.attachMovie("angryEyes", "zombie"+i, _root.getNextHighestDepth());
        addEnemy = enemyArray.push("zombie"+i);
    It is a string here not a movie clip..

    The post before where I said .push(Enemy) gave me NaN as output so.. I'm thinking I set up the array wrong or maybe something else?
    PHP Code:
    //Enemy Array
    var enemyArray = new Array();
    //Enemy function
    newEnemy = function () {
        var 
    Enemy _root.attachMovie("angryEyes""zombie"+i_root.getNextHighestDepth());
        
    addEnemy enemyArray.push("zombie"+i);
        
    i++;
    // more code here etc etc 
    Basically its the .push() stuff in the brackets is what could be the problem maybe? It has to mean the instance name of the newly created movie clip when function runs. Sorry if I am not making sense. I will keep reading and trying things.
    Last edited by t0w; April 22nd, 2008 at 04:27 PM.

  13. Variable names can't start with a capital letter. That's why it doesn't work.

    Arrays can store anything, even different variable types.

  14. #14
    Ah, thank you very much. I feel like a noob.
    It seems to be working better to an extent...
    Code:
    newEnemy = function () {
        var enemy = _root.attachMovie("angryEyes", "zombie"+i, _root.getNextHighestDepth());
        addEnemy = enemyArray.push(enemy);
        i++;
    Returns

    _level0.zombie[type Function]
    _level0.zombieNaN
    _level0.zombieNaN
    _level0.zombieNaN
    _level0.zombieNaN
    Last edited by t0w; April 22nd, 2008 at 04:48 PM.

  15. Oh my, of course this doesn't work, try it like this:

    enemyArray.push( _root.attachMovie("angryEyes", "zombie"+i, _root.getNextHighestDepth());
    i ++;


    It's good that you keep trying to do this yourself, but I don't have the feeling you know what you're doing Keep trying though, you're almost there!

Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012