I'm trying to throw an object and create the angle and velocity the object is moving based on the beginning touch point and end touch point. So far I am able to track the touch points while dragging the object mySprite. I also have static gravity in place for when the object is released. However, I'm having a hard time figuring out how to link the two together.
I have a function onTouchEnd() that detects when the object is released and that's where the gravity kicks in. But right now nothing happens.
What am I missing to be able to pinpoint the start and end touch points and then save the angle between the two for the object's trajectory? Sorry for being such a noob.
EDIT: updated code using localX, localY
package {
import flash.display.MovieClip;
import flash.events.*;
import flash.ui.Multitouch;
import flash.ui.MultitouchInputMode;
import flash.display.Sprite;
public class ThrowIt extends MovieClip{
public var bg:Sprite = new Sprite();
public var mySprite:Sprite = new Sprite();
Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
public var startX:int;
public var endX:int;
public var startY:int;
public var endY:int;
public var angle:int;
//public var speed:Number = 10; //this will change based on mouse speed
public var moving:Number = 0;
public var speedLimit:Number = 35;
public var scale_x:Number = Math.cos(angle);
public var scale_y:Number = Math.sin(angle);
public var posY:Number = stage.y;
public var goingUp:Boolean = false;
public var goingDown:Boolean = false;
//public var velocityX:Number = (speed*scale_x);
//public var velocityY:Number = (speed*scale_y);
public function ThrowIt() {
mySprite.graphics.beginFill(0x336699);
mySprite.graphics.drawRect(0,0,40,40);
//addChild(bg);
addChild(mySprite);
mySprite.addEventListener(TouchEvent.TOUCH_BEGIN, onTouchBegin);
mySprite.addEventListener(TouchEvent.TOUCH_END, onTouchEnd);
}
function onTouchBegin(e:TouchEvent) {
mySprite.startTouchDrag(e.touchPointID, false);
/*TRACK THE START POINT OF THE DRAG HERE*/
/*NEED THE X AND Y POSITION TO FIGURE OUT ANGLE AND VELOCITY OF OBJECT*/
startX = e.localX;
startY = e.localY;
trace("the starting x is " + startX);
}
function onTouchEnd(e:TouchEvent) {
mySprite.stopTouchDrag(e.touchPointID);
/*TRACK THE END POINT OF THE DRAG HERE*/
/*AGAIN, NEED BOTH THE X AND Y POSITION*/
endY = e.localY;
endX = e.localX;
//angle = (endX - startX)*Math.PI(180);
/*AFTER GETTING THE VELOCITY FIGURED OUT, THROW OBJECT*/
if(!goingUp){
goingUp = true;
//moving = speedLimit*-1;
moving = (startY - endY)*-1;
moving += .2;
mySprite.y += moving;
mySprite.y = Math.min(mySprite.y, stage.stageHeight - 510); //min position of the object
mySprite.y = Math.max(mySprite.y, 0); ///max position of the object
}
if(moving < 0){//if mySprite is still going up
moving *= 1 - speedLimit/250;//decrease moving slightly
if(moving > -speedLimit*.1){//if moving is small enough
moving *= -1;//then begin to go down
}
}
if(moving > 0 && moving <= speedLimit){//if mySprite is going down
moving *= 1 + speedLimit/400;//increase the falling speed
}
mySprite.y += moving;
//WHEN OBJECT GOES BELOW THE STAGE, DELETE IT AND STOP TRACKING EVENTS
if(mySprite.y >= stage.stageHeight){
removeChild(mySprite);
mySprite.removeEventListener(TouchEvent.TOUCH_END, onTouchEnd);
mySprite.removeEventListener(TouchEvent.TOUCH_BEGIN, onTouchBegin);
trace("removed");
}
}
}
}
Calculating your angle using just x value seem strange, try this:
This will give you the angle in radian.
For calculating the velocity you must have a timing variable between the start and the end touch. You can use
getTimer()to getbeginTimeandendTimein your functionsonTouchBeginandonTouchEndrespectively.After that you just have to calculate velocity with distance and time.
Hope that helps ;)