A Flash Developer Resource Site

Results 1 to 8 of 8

Thread: Wall Collision, Not Working?

  1. #1
    -
    Join Date
    Feb 2006
    Posts
    74

    Wall Collision, Not Working?

    Would anyone mind taking a look at this and seeing whats wrong with it?
    Im pretty new to flash and this is the first time i have looked at collisions.

    I saw an example that used lots of balls colliding on walls but I only want to see one. I can't seem to get this working. Any Ideas?


    First Frame Code
    Code:
    this._lockroot = true;
    var SW:Number = 530;
    var SH:Number = 300;
    var mcBall:MovieClip;
    
    _root.onEnterFrame = move;
    
    function move():Void{
    	mcBall = _root.ball;
    	mcBall._x =0;
    	mcBall._y =0;
    	checkWallCollisions();
    	
      
    }
    
    function checkWallCollisions():Void{
    	if (mcBall._x + mcBall._width / 2 > SW) {
    		mcBall._x = SW-mcBall._width / 2;
    		mcBall.vx *= -.9;
    		trace("collision");
    	} else if (mcBall._x - mcBall._width / 2 < 0) {
    		mcBall._x = 0+ mcBall._width / 2;
    		mcBall.vx *= -.9;
    		trace("collision");
    	}
    	if (mcBall._y + mcBall._width / 2 > SH) {
    		mcBall._y = SH-mcBall._width / 2;
    		mcBall.vy *= -.9;
    		trace("collision");
    	} else if (mcBall._y - mcBall._width / 2 < 0) {
    		mcBall._y = 0+mcBall._width / 2;
    		mcBall.vy *= -.9;
    		trace("collision");
    	}
    }
    Circle Movieclip Code
    Code:
    // Set the ships physics
    onClipEvent (load) {
    	// Sets the start X and Y speed of the ship
    		yspeed =0;
    		xspeed = 0;
    	// Vertical force
    		power = 0.9;
    	// Sets the horizontal resistance
    		friction = 0.95;
    	// Sets the force acting down on the ship
    		DownGravity = 0.1;
    	// Sets the ships thrust
    		thrust = 0.75;
    }
    
    //Ship Controls
    onClipEvent (enterFrame) {
    	if (Key.isDown(Key.LEFT)) {
    		xspeed -= power;
    	}
    	if (Key.isDown(Key.RIGHT)) {
    		xspeed += power;
    	}
    	if (Key.isDown(Key.UP)) {
    		yspeed -= power * thrust;
    	}
    	if (Key.isDown(Key.DOWN)) {
    		yspeed += power * thrust;
    	}
    	this.ymov = yspeed;
    	this.xmov = xspeed;
    	if (this.ymov != 0 || this.xmov != 0) {
    		_root.ballToBallDetection(this, _root.Target_mc);
    	}
    	checkWallCollisions();
    	xspeed *= friction;
    	yspeed += DownGravity;
    	_y += yspeed;
    	_x += xspeed;
    }
    Attached Files Attached Files
    Last edited by Owenbooty; 12-26-2006 at 12:21 PM. Reason: added code

  2. #2
    OOP is one letter from OOPS kortex's Avatar
    Join Date
    Aug 2005
    Location
    New Hope, PA
    Posts
    2,668
    This is out of an AS 3 script that I use to bounce several balls off of the stage boundaries. Should illustrate the idea enough to be converted to AS 2.0 (see bold code).

    package doc
    {
    import flash.display.Sprite;
    import flash.utils.Timer;
    import flash.events.Event;
    import flash.events.TimerEvent;
    import flash.display.Stage;
    import flash.filters.*;
    public class MovingCircle extends Sprite
    {
    private var tmr : Timer
    private var xDir : int = 1;
    private var yDir : int = 1;

    private var colur:uint;
    public function MovingCircle (colr : uint, radius : Number)
    {
    colur = colr;
    graphics.beginFill (colr, 1);
    graphics.drawCircle (0, 0, radius);
    graphics.endFill ();
    tmr = new Timer (30);
    tmr.addEventListener ("timer", drift);
    tmr.addEventListener ("timer", glow);
    tmr.start ();
    }
    private function drift (event : TimerEvent)
    {
    if (x < this.width/2)
    {
    x = this.width/2;
    xDir *= - 1;
    } else if (x > (parent.stage.stageWidth-(this.width/2)))
    {
    x = parent.stage.stageWidth-(this.width/2);
    xDir *= - 1;
    }
    if (y < this.height/2)
    {
    y = this.height/2;
    yDir *= - 1
    } else if (y > parent.stage.stageHeight-(this.height/2))
    {
    y = parent.stage.stageHeight-(this.height/2);
    yDir *= - 1;
    }
    x += xDir;
    y += yDir;
    }

    private function glow(event:TimerEvent){
    var gf:GlowFilter = new GlowFilter(colur, Math.random(), Math.random()*10, Math.random()*10, Math.random()*10, BitmapFilterQuality.HIGH, false, false)
    this.filters = [gf];
    }
    }
    }
    Jeremy Wischusen
    Flash - Flex - LAMP - Web Developer Purple Inc
    AS OOP FAQ-Best Practices Thread | Flashkit OOP Tutorials | Purple Inc (day job) | Blog


  3. #3
    -
    Join Date
    Feb 2006
    Posts
    74
    The collision code works as does the circle movement code but the dont seem to work together. If i just place the circle over the line with out the movment code it detects the collision
    Last edited by Owenbooty; 12-26-2006 at 12:19 PM.

  4. #4
    OOP is one letter from OOPS kortex's Avatar
    Join Date
    Aug 2005
    Location
    New Hope, PA
    Posts
    2,668
    Well this will get it moving:

    this._lockroot = true;
    var SW:Number = 530;
    var SH:Number = 300;

    var mcBall:MovieClip;
    mcBall = _root.ball;
    mcBall._x =100;
    mcBall._y =100;
    mcBall.vx = -.9;
    mcBall.vy = -.9;

    _root.onEnterFrame = checkWallCollisions;

    function checkWallCollisions():Void{
    //trace ("Ball x = " + mcBall._x + " Stage width = " + SW + " Ball y = " + mcBall._y + " Stage height = " + SH);
    if (mcBall._x + (mcBall._width / 2) > SW) {
    trace ("greater than stage width");
    mcBall._x = SW-mcBall._width / 2;
    mcBall.vx *= -.9;
    } else if (mcBall._x - (mcBall._width / 2) < 0) {
    trace ("less than stage width");
    mcBall._x = 0+ mcBall._width / 2;
    mcBall.vx *= -.9;
    }
    if (mcBall._y + (mcBall._width / 2) > SH) {
    trace ("greater than stage height");
    mcBall._y = SH-mcBall._width / 2;
    mcBall.vy *= -.9;
    } else if (mcBall._y - (mcBall._width / 2) < 0) {
    trace ("less than stage height");
    mcBall._y = 0+mcBall._width / 2;
    mcBall.vy *= -.9;
    }
    mcBall._x += mcBall.vx;
    mcBall._y+= mcBall.vy;
    trace ("Incriment by = " + mcBall.vx + mcBall.vy)
    }
    Jeremy Wischusen
    Flash - Flex - LAMP - Web Developer Purple Inc
    AS OOP FAQ-Best Practices Thread | Flashkit OOP Tutorials | Purple Inc (day job) | Blog


  5. #5
    -
    Join Date
    Feb 2006
    Posts
    74
    Hey Kortex,

    I have it moving see the fla at in the first post, but it does not detect the collision, could you take a look. I want it to detect the collsion using math and bounce it back off the wall.

  6. #6
    OOP is one letter from OOPS kortex's Avatar
    Join Date
    Aug 2005
    Location
    New Hope, PA
    Posts
    2,668
    Well the original one I downloaded did not have any code on the ball itself. The code i posted was a modified version of what I downloaded from the first post and the collision detection worked after I used the code I posted.
    Jeremy Wischusen
    Flash - Flex - LAMP - Web Developer Purple Inc
    AS OOP FAQ-Best Practices Thread | Flashkit OOP Tutorials | Purple Inc (day job) | Blog


  7. #7
    -
    Join Date
    Feb 2006
    Posts
    74
    Sorry, that was my fault changed it after you saw it.

    Take alook at the attatched, the collision works but the rection does not work that well, is there a way to make the object bouce more off the wall, i think its not working as it did becase of the addition of user controled movement.
    Attached Files Attached Files

  8. #8
    -
    Join Date
    Feb 2006
    Posts
    74
    Any one able to suggest a resolution? The attached collision detection/reaction works but when you add controls to move the circle with the arrows then the collision reaction stops working

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