我正在尝试投射一个物体,并根据开始的触摸点和结束触点创建物体移动的角度和速度。到目前为止,我可以在拖动对象mySprite
的同时跟踪触摸点。当物体被释放时,我也有静态重力。但是,我很难弄清楚如何将两者联系在一起。
我有一个函数onTouchEnd()
,它可以检测对象何时被释放,并且是重力开始的地方。但是现在没有任何反应。
我缺少什么能够精确定位开始和结束触点,然后保存两者之间的对象轨迹角度?很抱歉这样的菜鸟。
编辑:使用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");
}
}
}
}
答案 0 :(得分:0)
使用x值计算角度看起来很奇怪,试试这个:
angle = Math.atan( (startY - endY) / (startX - endX) ); // in rad
这将为您提供弧度角度。
为了计算速度,您必须在开始和结束触摸之间有一个时间变量。您可以使用getTimer()
分别在函数beginTime
和endTime
中获取onTouchBegin
和onTouchEnd
。
之后你只需要用距离和时间来计算速度。
distance = Math.sqrt( ((startX - endX)*(startX - endX)) / ((startY - endY)*(startY - endY)) );
velocity = distance / (endTime-beginTime); // in pixel/ms
希望有所帮助;)