A Flash Developer Resource Site

Page 2 of 3 FirstFirst 123 LastLast
Results 21 to 40 of 52

Thread: collision detection help

  1. #21
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Yes, exactly. My thingies array has 3 identical instances of DragThingy in it, but you could actually put any DisplayObjects in there.

    I'm not sure what snippet you'd like to see. It would just be something like "thingies.push(titleObj)" or "thingies.push(getChildByName('titleObj')", depending on what sort of references you've got.

  2. #22
    Senior Member
    Join Date
    Apr 2001
    Posts
    102
    so then if i have my items already laid out on the stage and have instance names i could just do the following then thingies.push(titleObj, previewObj, buttonObj)?

    They are all instance names so that should just push the instance names into an array right or would it be best to do thingies.push(getChildByName('titleObj'), getChildByName('previewObj'), getChildByName('buttonObj')) ?

  3. #23
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    If you have "automatically declare stage instances" set, then you've also got variables corresponding to those, so either of your statements should work.

    To go into more detail, the things you push into the array must be the actual DisplayObjects, not the names.
    thingies.push(titleObj, previewObj, buttonObj); //should work, because those are variables of type DisplayObject;
    thingies.push(getChildByName('titleObj'), getChildByName('previewObj'), getChildByName('buttonObj')); //should work, because getChildByName returns DisplayObject
    thingies.push('titleObj', 'previewObj', 'buttonObj'); //should NOT work, because those are Strings.

  4. #24
    Senior Member
    Join Date
    Apr 2001
    Posts
    102
    i guess my problem is this. what do i make t = to? I know i can push the items to the array but then do i make t = to each item in the array?

    for (var i:int = 0; i < 3; i++){
    t = new DragThingy();
    addChild(t);
    t.x = i*55;
    thingies.push(titleObj, previewObj, buttonObj);
    }

  5. #25
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    You don't need t or DragThingy at all. I only had it because I needed something to drag around in my example.

    So, get rid of that entire for loop, and put the thingies.push statement in some initialization function.

  6. #26
    Senior Member
    Join Date
    Apr 2001
    Posts
    102
    so i did the taking out of the for loop but you have a reference to dragThingy in the if statement on the mouseDown function. What do i make that if statement equal too now then?

    Code:
    public function Main():void
    		{
    			
    			thingies = new Array();
    			thingies.push(getChildByName('titleObj'), getChildByName('previewObj'), getChildByName('buttonObj'));
    			addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
    			addEventListener(MouseEvent.MOUSE_UP, mouseUp);
    		}
    		
    		public function mouseDown(event:MouseEvent):void{
    			var obj:Object = event.target;
    			if (obj is DragThingy){
    				currentDragged = Sprite(obj);
    				currentDragged.startDrag();
    				lastGoodPos = getPoint(currentDragged);
    				addEventListener(MouseEvent.MOUSE_MOVE, mouseMove);
    			}
    		}
    		
    		public function mouseUp(event:MouseEvent = null):void{
    			if (null != currentDragged){
    				currentDragged.stopDrag();
    				currentDragged = null;
    				removeEventListener(MouseEvent.MOUSE_MOVE, mouseMove);
    			}
    		}

  7. #27
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    You can use Sprite.

    I've also just noticed a potential problem when the draggables are complex objects. It may be advantageous to add the mousedown listener to each of your draggable things and use currentTarget instead of target in the mousedown function.

  8. #28
    Senior Member
    Join Date
    Apr 2001
    Posts
    102
    now i guess the next step/issue is i have a set area of boundries i don't want all the objects to be draggable out of. How would one go about defining that area for ALL the objects so that if drag to the edge it would do a collision detection just like the other objects.

    I guess almost like getBounds which i think use to be an old method in AS 2.

  9. #29
    Senior Member
    Join Date
    Apr 2001
    Posts
    102
    i also am getting the following error every time i move an object even though it seems to work fine.

    TypeError: Error #2007: Parameter hitTestObject must be non-null.
    at flash.display:isplayObject/flash.displayisplayObject::_hitTest()
    at flash.display:isplayObject/hitTestObject()
    at Main/mouseMove()

    my code looks like this

    Code:
    	public function mouseMove(event:MouseEvent):void{
    			var d:DisplayObject;
    			var hitSomething:Boolean = false;
    			for (var i:int = 0; i < thingies.length; i++){
    				d = thingies[i];
    				if (currentDragged == d){
    					continue;
    				}
    				if (null != currentDragged){
    				  if(currentDragged.hitTestObject(d)){
    					trace("ouch!  stop hitting me!");
    					var c:Sprite = currentDragged;
    					mouseUp(); //should also be your mouseUp handler when dragging.
    					//here, we really should resolve any residual overlap between c and d
    					//FlashConnect.trace("bad pos: "+getPoint(c));
    					setPosition(c, lastGoodPos);
    					hitSomething = true;
    				  }
    				}
    			}
    			if (!hitSomething){
    				lastGoodPos = getPoint(currentDragged);  
    				//FlashConnect.trace("lastGoodPos set to: "+lastGoodPos);
    			}
    		}

  10. #30
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Second problem first: It appears you've managed to put null in the thingies array somehow. You could test for it.
    Code:
    ...
    if ((null != currentDragged) && (null != d)){
    ...
    But, I suggest not putting null in there in the first place if you can help it.

    The first problem could be solved by also testing that currnetDragged is fully on the stage.
    Code:
    ...
    if (currentDragged.hitTestObject(d) || !completelyOnStage(currentDragged)){
    ...
    completelyOnStage can be a simple function:
    Code:
    function completelyOnStage(obj:DisplayObject):Boolean{
      return (obj.x >=0 0) && (obj.y >=0) && (obj.x+obj.width < stage.stageWidth) && (obj.y+obj.height < stage.stageHeight);
    }

  11. #31
    Senior Member
    Join Date
    Apr 2001
    Posts
    102
    so im confused to where in the completelyOnStage function i would put in my boundery cordinates?

    Do i put it in obj.x >=0 and say like obj.x >=64 and the same for y coordinate?

    Doesn't it need to get the starting X cordinate then the ending X coordinate then the starting Y coordinate and ending Y coordinate

  12. #32
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Oops. I thought you wanted the stage to be the boundary. Forget that function, but you can write a similar, more general one.
    Code:
    function checkBounds(obj:DisplayObject):Boolean{
      //assumes you have a boundsObj on stage.  Consider saving Rectangle directly.
      var checkRect:Rectangle = boundsObject.getBounds(stage);
      return checkRect.containsRect(obj.getBounds(stage));
    }

  13. #33
    Senior Member
    Join Date
    Apr 2001
    Posts
    102
    i tried the above and with the following code i get three errors.

    Code:
    public function mouseMove(event:MouseEvent):void{
    			var d:DisplayObject;
    			var hitSomething:Boolean = false;
    			for (var i:int = 0; i < draggableItems.length; i++){
    				d = draggableItems[i];
    				if (currentDragged == d){
    					continue;
    				}
    				if ((null != currentDragged) && (null != d)){
    				  if(currentDragged.hitTestObject(d)){
    					trace("ouch!  stop hitting me!");
    					var c:Sprite = currentDragged;
    					mouseUp(); //should also be your mouseUp handler when dragging.
    					//here, we really should resolve any residual overlap between c and d
    					trace("bad pos: "+getPoint(c));
    					setPosition(c, lastGoodPos);
    					hitSomething = true;
    				  }
    				}
    				if (currentDragged.hitTestObject(d) || !checkBounds(currentDragged)){
    					checkBounds();
    				}
    			}
    			if (!hitSomething){
    				lastGoodPos = getPoint(currentDragged);  
    				trace("lastGoodPos set to: "+lastGoodPos);
    			}
    		}
    		
    		private function checkBounds(obj:DisplayObject):Boolean{
    		  //assumes you have a boundsObj on stage.  Consider saving Rectangle directly.
    		  var checkRect:Rectangle = boundsObject.getBounds(stage);
    		  return checkRect.containsRect(obj.getBounds(stage));
    		}
    Code:
    **Error** /Users/Colin/Desktop/widgetCreator/Main.as, Line 64: 1136: Incorrect number of arguments.  Expected 1.
         checkBounds();
    
    **Error** /Users/Colin/Desktop/widgetCreator/Main.as, Line 75: 1046: Type was not found or was not a compile-time constant: Rectangle.
         var checkRect:Rectangle = boundsObject.getBounds(stage);
    
    **Error** /Users/Colin/Desktop/widgetCreator/Main.as, Line 75: 1120: Access of undefined property boundsObject.
         var checkRect:Rectangle = boundsObject.getBounds(stage);

  14. #34
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    import Rectangle at the top.
    Code:
    import flash.geom.Rectangle;
    As the function says, it assumes you have a boundsObject. You can replace "boundsObject" with whatever you actually want to test against.

    And lastly, I'm not sure what you're doing with the checkBounds call. It should look more like this:
    Code:
    public function mouseMove(event:MouseEvent):void{
    			var d:DisplayObject;
    			var hitSomething:Boolean = false;
    			for (var i:int = 0; i < draggableItems.length; i++){
    				d = draggableItems[i];
    				if (currentDragged == d){
    					continue;
    				}
    				if ((null != currentDragged) && (null != d)){
    				  if(currentDragged.hitTestObject(d) || !checkBounds(currentDragged)){
    					trace("ouch!  stop hitting me!");
    					var c:Sprite = currentDragged;
    					mouseUp(); //should also be your mouseUp handler when dragging.
    					//here, we really should resolve any residual overlap between c and d
    					trace("bad pos: "+getPoint(c));
    					setPosition(c, lastGoodPos);
    					hitSomething = true;
    				  }
    				}
    			}
    			if (!hitSomething){
    				lastGoodPos = getPoint(currentDragged);  
    				trace("lastGoodPos set to: "+lastGoodPos);
    			}
    		}
    		
    		private function checkBounds(obj:DisplayObject):Boolean{
    		  //assumes you have a boundsObj on stage.  Consider saving Rectangle directly.
    		  var checkRect:Rectangle = boundsObject.getBounds(stage);
    		  return checkRect.containsRect(obj.getBounds(stage));
    		}

  15. #35
    Senior Member
    Join Date
    Apr 2001
    Posts
    102
    where do initiate the function though cause its not initiated anywhere.

  16. #36
    Senior Member
    Join Date
    Apr 2001
    Posts
    102
    neveremind i got it, im stupid.

    Thanks so much for all this help so far i greatly appreciate it.

  17. #37
    Senior Member
    Join Date
    Apr 2001
    Posts
    102
    how would one make it so that certain objects i click on are draggable and some arn't? would it be best to create an array of object names i don't want draggable? How would one go about that.

    I ask this cause i have like 10 buttons total. One button is an info button that i don't want draggable and the rest i do. In addition i also have a movieclip that acts as a container which contains three other movieclips. I only want the container dragable and not each individual one inside the container draggable.

    As i said above how would i set a list of objects that are not draggable?

  18. #38
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    You could check thingies instead of that they're sprites.
    Code:
    if (obj is Sprite)
    becomes
    Code:
    if (thingies.indexOf(obj) != -1)
    That will make it so that only the things you are testing for collisions are draggable. If you have things that you want to check for collisions that you don't want draggable, you'd have to do it differently.

  19. #39
    Senior Member
    Join Date
    Apr 2001
    Posts
    102
    yes i have things that i don't want draggable that i want to check for collision.

    maybe i am wrong but what i have is a movieclip called btnGroup that contains three buttons called btn1, btn2, btn3. I want btn group to be draggable and not btn1 etc and then only have btnGroup detect for collision.

    Would the previous code be the best way to do it still?

  20. #40
    Senior Member
    Join Date
    Apr 2001
    Posts
    102
    i tried doing the code above and it seems to only be dragging 1 item in the array of thingies (which i renamed to draggableItems).

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