A Flash Developer Resource Site

Results 1 to 8 of 8

Thread: High school student needing Flash help!

Hybrid View

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    3

    Unhappy High school student needing Flash help!

    Hi everyone,

    Could you please take 5 minutes to read through my problem, it's not even that hard (I dont think) but I only have high school programming knowledge and I'm stuck. It's kindof lengthy but bear with me


    as my title states, I am a high school student and I'm making a project for my programming class. Im making an iPhone game that essentially lets the user pull back a ball from a given point and once they let go, the ball will move in the other direction of their pull. The ball has to hit a target and the next level(s) will be the same except with obstacles and differently placed targets.

    I have already completed the majority of the project, i.e. I have figured out how to calculate an angle and the ball moves beautifully in my desired direction, HOWEVER, since I am using triangles to move my ball, whenever I hit the left or right side of the wall and I flip the angle, the ball moves in the right direction but it starts at a lower or higher part of the screen. I have figured out how to fix this problem and I figured out that the ball is being offset due to the triangle, and every time it hits the left or right wall I simply add the offset to it's y value.

    now my problem is that as soon as I hit the top or bottom wall, the offset that was previously calculated alters where the ball will go and I can't make it hit the top or bottom with a smooth bounce. Can anybody help me with this problem??

    If needed I can post my flash file and you can look through the code, it's about 200 lines long.

    Thanks in advance

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    What do you mean by "using triangles to move my ball"?
    Post the relevant code. By which I mean any code you're using to define the Ball if it's more than just a sprite with graphics, and your collision/reflection code.

    Or post all 200 lines.

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    3
    okay I'll try to break it down..
    this first part is my functions for detecting if the mouse is on the ball and if it is not then I have a boolean variable which is false, otherwise it is true and the ball moves with the mouse. Then as soon as the mouse is up again, the boolean becomes false and the next function (handleMouseUp) is executed
    Actionscript Code:
    stop();

    var MouseOnBall:Boolean;
    var xcurrent:Number;
    var ycurrent:Number;
    var bounceAngle:Number;
    var invertedAngle:Number;
    var Dan:Number;
    var xmove:int = 1;
    var finalx:Number;
    var finaly:Number;
    var xreverse:int = 1;
    var yreverse:int = 1;
    var mycountery, mycounterx: int;
    var yoffset:int = 0;
    var b:int = 0;

    this.addChild(fingerBall);
     fingerBall.mouseEnabled = false;
     fingerBall.x = stage.stageWidth * 0.5;
     fingerBall.y = stage.stageHeight * 0.5;

    stage.addEventListener(MouseEvent.MOUSE_DOWN, handleMouseDown);

    function handleMouseDown(e:MouseEvent)
    {
           if (  ((fingerBall.x > (e.target.mouseX - 25)) && (fingerBall.x <
    (e.target.mouseX + 25))) &&
             ((fingerBall.y > (e.target.mouseY - 25)) && (fingerBall.y <
    (e.target.mouseY + 25)))    )
           {
                   MouseOnBall = true;
           }
           else
           {
                   MouseOnBall = false;
           }
            stage.addEventListener(MouseEvent.MOUSE_MOVE, handleMouseMove);

            stage.addEventListener(MouseEvent.MOUSE_UP, handleMouseUp);

    }

    function handleMouseMove(e:MouseEvent)
    {
           if (MouseOnBall == true)
           {
                   fingerBall.x = e.target.mouseX;
                   fingerBall.y = e.target.mouseY;
           }
    }

    the main function which moves the ball with a timer and also has the bounce:
    function handleMouseUp(e:MouseEvent)
    Actionscript Code:
    {
            stage.removeEventListener(MouseEvent.MOUSE_MOVE, handleMouseMove);
            stage.removeEventListener(MouseEvent.MOUSE_UP, handleMouseUp);

            if (MouseOnBall == true)
            {

                    var angle = Math.atan2((fingerBall.y - (stage.stageHeight * 0.5)),
    (fingerBall.x - (stage.stageWidth * 0.5)));

                    angle = angle * (180/Math.PI);

                    var MoveTimer:Timer = new Timer(2);
                    MoveTimer.addEventListener(TimerEvent.TIMER, MoveBall)
                    function MoveBall (e:TimerEvent):void
                    {
                            if ((fingerBall.y >= 25) && (fingerBall.y <= 453) && (fingerBall.x
    >= 21) && (fingerBall.x <= 303))
                            {
                                   xcurrent = (fingerBall.x - (stage.stageWidth * 0.5) - (xmove * xreverse));
                                   ycurrent = (xcurrent * Math.tan((invertedAngle * (Math.PI/180))));
                                   fingerBall.x = (stage.stageWidth * 0.5) + xcurrent;

                                   if ((angle >= 90) && (angle <= 180))
                                   {
                                           fingerBall.y = (stage.stageHeight * 0.5) - (ycurrent * yreverse) - yoffset;

                                           if (mycounterx > 1)
                                           {
                                                   fingerBall.y = (stage.stageHeight * 0.5) - (ycurrent * yreverse)
    - yoffset;
                                           }
                                   }

                                   if ((angle >= -90) && (angle <= 0))
                                   {
                                           fingerBall.y = (stage.stageHeight * 0.5) - (ycurrent * yreverse) - yoffset;

                                           if (mycounterx > 1)
                                           {
                                                   fingerBall.y = (stage.stageHeight * 0.5) - (ycurrent * yreverse)
    - yoffset;
                                           }
                                   }

                                   else
                                   {
                                           fingerBall.y = (stage.stageHeight * 0.5) + (ycurrent * yreverse) - yoffset;

                                           if (mycounterx > 1)
                                           {
                                                   fingerBall.y = (stage.stageHeight * 0.5) + (ycurrent * yreverse)
    - yoffset;
                                           }
                                   }
                            }

                            if ((fingerBall.x <= 22) || (fingerBall.x >= 302))
                            {
                                    invertedAngle *= -1;
                                    xreverse *= -1;
                                    mycountery = 1;
                                    mycounterx += 1;
                                    if (mycounterx == 1)
                                    {
                                            yoffset = ((((stage.stageHeight * 0.5) ) - fingerBall.y) * 2);
                                    }
                                    if (mycounterx > 1)
                                    {
                                            yoffset *= (mycounterx/(mycounterx - 1));
                                    }
                                    trace ("Mycounterx: " + mycounterx);
                                    trace ("yoffset: " + yoffset);
                                    trace ("y after:" + fingerBall.y);
                                    finaly = fingerBall.y;
                                    //trace ("Last y: " + finaly)
                            }
                            if ((fingerBall.y <= 27) || (fingerBall.y >= 452))
                            {
                                    finalx = fingerBall.x;
                                    trace ("Finalx: " + finalx);
                                    invertedAngle *= -1;
                                    b = ((stage.stageWidth - finalx) * (Math.tan(invertedAngle *
    (Math.PI/180))))
                                    //xreverse *= -1;
                                    //xmove *= -1;
                                    mycounterx = 1;
                                    mycountery += 1;
                                    if (mycountery == 1)
                                    {
                                            yoffset = 0;
                                    }
                                    if (mycountery > 1)
                                    {
                                            //yoffset *= ((mycounterx)/(mycounterx - 1));
                                            //yoffset -= (yoffset - (stage.stageHeight * 0.5) + 30);
                                    }

                            }
                            /*if (mycounter > 8)
                            {
                                   MoveTimer.stop()
                                   xmove = 1;
                                   invertedAngle = angle;
                                   mycounter = 0;
                                   yreverse = 1;
                                   xreverse = 1;
                                   yoffset = 0;
                            }*/

                    }

                    if ((angle >= 0) && (angle <= 90))
                            {
                                    invertedAngle = angle;
                                    xmove = 1;
                                    MoveTimer.start();
                            }
                    if ((angle >= 90) && (angle <= 180))
                            {
                                    invertedAngle = angle;
                                    xmove = -1;
                                    MoveTimer.start();
                            }
                    if ((angle >= -180) && (angle <= -90))
                            {
                                    invertedAngle = (angle + 180);
                                    xmove = -1;
                                    MoveTimer.start();
                            }
                    if ((angle >= -90) && (angle <= 0))
                            {
                                    invertedAngle = (angle * -1);
                                    xmove = 1;
                                    MoveTimer.start();
                            }
                    trace (angle);
                    /*stage.removeEventListener(MouseEvent.MOUSE_DOWN, handleMouseDown);
                    stage.removeEventListener(MouseEvent.MOUSE_MOVE, handleMouseMove);
                    stage.removeEventListener(MouseEvent.MOUSE_UP, handleMouseUp);*/

                    //xreverse = 1;
                    //yreverse = 1;
                    mycounterx = 0;
                    mycountery = 0;

            }
    }

    you'll see that my yoffset is twice the difference between half the stage height and the ball's y variable. its kindof complicated in my opinion but hopefully you get it, and any questions just ask me.

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    That looks extremely complicated and I can't quite tell what you're trying to do. I do see a bunch of stuff you shouldn't be doing, though.

    Don't define MoveBall inside handleMouseUp. If you do that, then each time handleMouseUp runs, a new Timer and a new MoveBall function is created. Since you never stop your timer or remove your timer listener, these will add up. But since your timer is a locally scoped variable and so is your MoveBall function, those will spontaneously disappear when the garbage collector eventually runs.

    A Timer with a delay of 2ms is trying for an effective framerate of 500fps. That's not going to happen. I don't see why you're using a Timer at all, rather than an ENTER_FRAME handler.

    You should learn and follow common syntactical conventions. The only things that should start with a capital letter are classes. Variables, functions, methods, whatever should all begin with lowercase letters.

    You should give all your variables strong types. You have completely left out types for angle, and return types for most of your functions.

    If you're just trying to get simple bounces off of walls, then all this is way overkill, and probably not even correct. All you need to do is keep a velocity (usually in the form of x and y move components) for your ball, then detect if the ball hits a wall. If it hits a vertical wall (x is too small or too big), then multiply the x component by -1. If it hits a horizontal wall (y is too small or too big), then multiply the y component by -1.

    Don't use magic numbers in your calculations. Where do 25, 453, 21, 303 come from? Those are probably dimensions of your stage or ball. Use the appropriate width or height properties. If you absolutely must have magic numbers, then at least define them once as variables/constants and use that.
    You shouldn't have to manually check bounding boxes like that anyway. There are methods for that. Add handleMouseDown to the ball rather than the stage and it'll only run when the mouse was on the ball. You'll still want the mouseUp to be on the stage in case the mouse is NOT on the ball when released.
    You can use startDrag() and stopDrag() instead of manually setting the ball's coordinates to the mouse's on mouseMove.

    I don't see any triangles here, and I don't understand why you're calculating angles all over the place. By "using triangles" you probably meant "using trigonometry" and I still don't see why that's necessary. Your collisions are entirely with either vertical or horizontal surfaces, right?

  5. #5
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Here's a demo I whipped up. You can fling the ball by dragging it around. If you want to slow it down, hold down the mouse on anywhere that isn't the ball.

    Ball.as:
    Code:
    package  
    {
    	import flash.display.Sprite;
    	import flash.events.Event;
    	import flash.events.MouseEvent;
    	
    	/**
    	 * ...
    	 * @author srs
    	 */
    	public class Ball extends Sprite 
    	{
    		public var prevX:Number;
    		public var prevY:Number;
    		public var xstep:Number;
    		public var ystep:Number;
    		
    		public function Ball() 
    		{
    			prevX = x;
    			prevY = y;
    			xstep = 0; 
    			ystep = 0;
    			graphics.beginFill(0xff0000);
    			graphics.drawCircle(0, 0, 15);
    			graphics.endFill();
    		}
    		
    		public function move():void {
    			x += xstep;
    			y += ystep;
    		}
    		
    	}
    
    }
    Main.as:
    Code:
    package 
    {
    	import flash.display.Sprite;
    	import flash.events.Event;
    	import flash.events.MouseEvent;
    	
    	/**
    	 * ...
    	 * @author srs
    	 */
    	public class Main extends Sprite 
    	{
    		private var ball:Ball;
    		private var ballDrag:Boolean;
    		
    		public function Main():void 
    		{
    			if (stage) init();
    			else addEventListener(Event.ADDED_TO_STAGE, init);
    		}
    		
    		private function init(e:Event = null):void 
    		{
    			removeEventListener(Event.ADDED_TO_STAGE, init);
    			// entry point
    			ballDrag = false;
    			ball = new Ball();
    			ball.x = (Math.random() * (stage.stageWidth - ball.width)) + ball.width/2;
    			ball.y = (Math.random() * (stage.stageHeight - ball.height)) + ball.height / 2;
    			addChild(ball);
    			ball.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
    			addEventListener(Event.ENTER_FRAME, enterFrame);
    			stage.addEventListener(MouseEvent.MOUSE_DOWN, startBrake);
    			stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMove);
    			stage.addEventListener(MouseEvent.MOUSE_UP, mouseUp);
    		}
    		
    		private function enterFrame(e:Event):void {
    			if (!ballDrag){
    				ball.move();
    				if (ball.x < ball.width/2 || ball.x > (stage.stageWidth - ball.width/2)) {
    					ball.x -= ball.xstep;
    					ball.xstep *= -1;
    				}
    				if (ball.y < ball.height/2 || ball.y > (stage.stageHeight - ball.height/2)) {
    					ball.y -= ball.ystep;
    					ball.ystep *= -1;
    				}
    			}
    		}
    		
    		/**
    		 * when we mousedown not on the ball, slow down the ball.
    		 * @param	me
    		 */
    		private function startBrake(me:MouseEvent):void {
    			addEventListener(Event.ENTER_FRAME, brake);
    			stage.addEventListener(MouseEvent.MOUSE_UP, stopBrake);
    		}
    		
    		private function brake(e:Event):void {
    			ball.xstep *= 0.9;
    			ball.ystep *= 0.9;
    		}
    		
    		private function stopBrake(me:MouseEvent):void {
    			removeEventListener(Event.ENTER_FRAME, brake);
    			stage.removeEventListener(MouseEvent.MOUSE_UP, stopBrake);
    		}
    		
    		private function mouseDown(me:MouseEvent):void {
    			ballDrag = true;
    			me.stopImmediatePropagation();
    		}
    		
    		private function mouseMove(me:MouseEvent):void {
    			if (ballDrag) {
    				ball.prevX = ball.x;
    				ball.prevY = ball.y;
    				ball.x = me.stageX;
    				ball.y = me.stageY;
    				ball.xstep = ball.x - ball.prevX;
    				ball.ystep = ball.y - ball.prevY;
    			}
    		}
    		
    		private function mouseUp(me:MouseEvent):void {
    			ballDrag = false;
    		}
    		
    	}
    	
    }

  6. #6
    Registered User
    Join Date
    May 2012
    Posts
    3
    Wow okay that was actually an extremely helpful response, go flashkit forums

    I just tried out your solution, and whenever you let go of the ball it begins moving horizontally or vertically whereas in mine it has to follow a specific path that the user essentially makes. In mine, the ball spawns in the centre of the screen, you can see that when I give its coordinates to be half of the height and width. From there, the user pulls the ball back and depending on what quadrant the ball is in (it got pretty messy with my angles as you can see) I tell it to either add or subtract x, as well as make the angle appropriate for the trigonometric calculation.

    What my problem is, is that since I let go of the ball and it travels through the middle spawning point, as soon as I hit the left or right wall the ball is automatically moved up or down so that it passes through the middle point again. I have fixed this left or right wall problem by figuring out the amount that it moves up or down and I call this the yoffset. as soon as I hit the top or bottom though, the yoffset tells the ball to keep moving off of the screen and I need it to change. I've tried everything...calculating a different offset, making it equal to zero, reversing it, but really not much progress..

    oh and there was also a time when I was trying to do everything mentioned above through linear equations, i.e. I would calculate a gradient and add the appropriate y and x values to the ball that lie along the line, then once I hit the wall it would times the gradient by -1 but this didnt work and i went back to the angle idea..

  7. #7
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    You should be able to release the ball with any direction in mine, not just horizontal or vertical.

    What is the desired behavior? Do you want it like my demo, but with a pull-and-release-to-launch mechanism?

  8. #8
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Revised Main, with pull-and-release-to-launch:
    Main:
    Code:
    package 
    {
    	import flash.display.Sprite;
    	import flash.events.Event;
    	import flash.events.MouseEvent;
    	import flash.geom.Point;
    	
    	/**
    	 * ...
    	 * @author srs
    	 */
    	public class Main extends Sprite 
    	{
    		private var ball:Ball;
    		private var ballDrag:Boolean;
    		//we'll draw our force vector visualization into here.
    		private var forceLayer:Sprite;
    		//where the dragging starts.  Used to calculate velocity on release
    		private var origin:Point;
    		//scaling factor for calculating force.
    		private var forceScale:Number = 0.2;
    		
    		
    		public function Main():void 
    		{
    			if (stage) init();
    			else addEventListener(Event.ADDED_TO_STAGE, init);
    		}
    		
    		private function init(e:Event = null):void 
    		{
    			removeEventListener(Event.ADDED_TO_STAGE, init);
    			// entry point
    			forceLayer = new Sprite();
    			addChild(forceLayer);
    			ballDrag = false;
    			ball = new Ball();
    			ball.x = stage.stageWidth / 2;
    			ball.y = stage.stageHeight / 2;
    			addChild(ball);
    			ball.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
    			addEventListener(Event.ENTER_FRAME, enterFrame);
    			stage.addEventListener(MouseEvent.MOUSE_DOWN, startBrake);
    		}
    		
    		private function enterFrame(e:Event):void {
    			if (!ballDrag){
    				ball.move();
    				if (ball.x < ball.width/2 || ball.x > (stage.stageWidth - ball.width/2)) {
    					ball.x -= ball.xstep;
    					ball.xstep *= -1;
    				}
    				if (ball.y < ball.height/2 || ball.y > (stage.stageHeight - ball.height/2)) {
    					ball.y -= ball.ystep;
    					ball.ystep *= -1;
    				}
    			}
    		}
    		
    		/**
    		 * when we mousedown not on the ball, slow down the ball.
    		 * @param	me
    		 */
    		private function startBrake(me:MouseEvent):void {
    			addEventListener(Event.ENTER_FRAME, brake);
    			stage.addEventListener(MouseEvent.MOUSE_UP, stopBrake);
    		}
    		
    		private function brake(e:Event):void {
    			ball.xstep *= 0.9;
    			ball.ystep *= 0.9;
    		}
    		
    		private function stopBrake(me:MouseEvent):void {
    			removeEventListener(Event.ENTER_FRAME, brake);
    			stage.removeEventListener(MouseEvent.MOUSE_UP, stopBrake);
    		}
    		
    		private function mouseDown(me:MouseEvent):void {
    			origin = new Point(ball.x, ball.y);
    			ball.startDrag();
    			ballDrag = true;
    			stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMove);
    			stage.addEventListener(MouseEvent.MOUSE_UP, mouseUp);
    			me.stopImmediatePropagation();
    		}
    		
    		private function mouseMove(me:MouseEvent):void {
    			forceLayer.graphics.clear();
    			forceLayer.graphics.lineStyle(1, 0);
    			forceLayer.graphics.moveTo(ball.x, ball.y);
    			forceLayer.graphics.lineTo(origin.x, origin.y);
    		}
    		
    		private function mouseUp(me:MouseEvent):void {
    			ball.stopDrag();
    			ballDrag = false;
    			stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMove);
    			stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUp);
    			//calculate force vector
    			ball.xstep = (origin.x - me.stageX) * forceScale;
    			ball.ystep = (origin.y - me.stageY) * forceScale;
    			forceLayer.graphics.clear();
    		}
    		
    	}
    	
    }

Tags for this Thread

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