A Flash Developer Resource Site

Results 1 to 13 of 13

Thread: Hittesting an array

  1. #1
    Member
    Join Date
    Sep 2006
    Posts
    82

    Hittesting an array

    I have a movieclip which is created every time the left key is pressed, and I was wondering how I would go about hittesting all of them.

    Code:
    Creating movieclip:
    if(Key.isDown(Key.SPACE)){
    for(k=0;k<=100;k++){
    		lb[k] = attachMovie("left","left"+k,depth++)
    }
    }
    
    Attempt at hittesting:
    for(n=0;n<=100;n++){
    	if (slider.hitTest("left"+[n])){ 
    	trace("left")
    }
    }

  2. #2
    Member
    Join Date
    Sep 2006
    Posts
    82
    I've changed it to this and I'm only getting left0 (lb0)to work..

    Actionscript Code:
    for(k=0;k<=100;k++){
    lb[k].onEnterFrame=function(){
        if(this.hitTest(_root.slider)){
            trace("left")
        }
    }

  3. #3
    :
    Join Date
    Dec 2002
    Posts
    3,518
    Maybe you can adapt this...
    Code:
    var k = 0;
    var lb = new Array();
    this.onEnterFrame = function() {
    	var len = lb.length;
    	if (Key.isDown(Key.SPACE)) {
    		if (len<100) {
    			len = lb.push(attachMovie("left", "left"+k, k++, {_x:3*k, _y:3*k}));
    		}
    	}
    	for (var j = len; j>=0; --j) {
    		if (lb[j].hitTest(_root.slider)) {
    			trace(lb[j]._name);
    			lb[j].removeMovieClip();
    			lb.splice(j,1);
    		}
    	}
    };

  4. #4
    Member
    Join Date
    Sep 2006
    Posts
    82
    Cool, now I have adapted that code in, and the hittest works, but the movieclips aren't showing up. I've tried to change the depth variable, but I'm still without luck. Anyone know what's the problem?

  5. #5
    :
    Join Date
    Dec 2002
    Posts
    3,518
    Post the code that you are now using...

  6. #6
    Member
    Join Date
    Sep 2006
    Posts
    82
    Actionscript Code:
    Attaching:
    var len = lb.length;
            if (len<100) {
                len = lb.push(attachMovie("left", "left"+k, depth++, {_x:3*k, _y:3*k}));
            }
    Placement:
    for(j=0;j<=100;++j){
            lb[j]._x = lxstart
            lb[j]._y = 27
            }
    Hittest:
    for(j=lb.length;j>=0;--j){
        if(lb[j].hitTest(_root.slider)){
            trace("hit")
        }
        }

    lxstart = slider._x

  7. #7
    :
    Join Date
    Dec 2002
    Posts
    3,518
    Code:
    //Attaching:
    var len = lb.length;
    // k needs to be initialized before use
    var k=0;
    // also make sure depth is initialized prior to use
    if (len<100) {
    	len = lb.push(attachMovie("left", "left"+k++, depth++, {_x:3*k, _y:3*k}));
    }
    //Placement:  
    for (j=0; j<=100; ++j) {
    	lb[j]._x = lxstart;
    	lb[j]._y = 27;
    }
    //Hittest:
    for (j=lb.length; j>=0; --j) {
    	if (lb[j].hitTest(_root.slider)) {
    		trace("hit");
    	}
    }

  8. #8
    Member
    Join Date
    Sep 2006
    Posts
    82
    i already have both those variables initialized in onLoad=function(){
    Last edited by sora72; 02-13-2010 at 03:51 AM.

  9. #9
    Member
    Join Date
    Sep 2006
    Posts
    82
    I'm still unable to find out the problem as to why each movieclip "deletes" the one before it. Does anyone have a clue whats wrong?

  10. #10
    Programmer
    Join Date
    Aug 2007
    Posts
    173
    Code:
    var len = lb.length;
    var k=0;
    
    if (len<100) { 
    	k++
    	len = lb.push(attachMovie("left", "left"+k, this.getNextHighestDepth(), {_x:3*k, _y:3*k}));
    }
    //Placement:  
    for (j=0; j<=100; j++) {
    	lb[j]._x = lxstart;
    	lb[j]._y = 27;
    }
    //Hittest:
    for (j=lb.length; j>=0; j--) {
    	if (lb[j].hitTest(_root.slider)) {
    		trace("hit");
    	}
    }
    try this.... not sure how you have this set out so if it doesnt work, try changing the this.getNextHighestDepth() to _root.getNextHighestDepth()
    Freelance: AS2, AS3, PHP, MySQL, JavaScript
    Skype: hunty93

  11. #11
    Member
    Join Date
    Sep 2006
    Posts
    82
    the depth isn't the problem. it has something to do with the placement. it's causing all the attached movies to move to the latest placement.

  12. #12
    Programmer
    Join Date
    Aug 2007
    Posts
    173
    can you post the .fla? ,.. just makes it easier to find the problem.
    Freelance: AS2, AS3, PHP, MySQL, JavaScript
    Skype: hunty93

  13. #13
    Member
    Join Date
    Sep 2006
    Posts
    82
    sorry... it's been fixed.

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