-
Drag & Rotate & throw Multiple MCs
I am trying to create a drag and throw effect I have most of the effect working correctly but I am wondering how to only move one item at a time and have that thrown unaffected til the friction brings the item to a stop. what happens currently is if an mc is moving and you select another it moves both items. any help is appreciated.
Code:
var mx0; // Previous Mouse X Position
var my0; // Previous Mouse Y Position
var velocityX = 0; // Velocity in X Direction
var velocityY = 0; // Velocity in Y Direction
var velocityR = 0; // Velocity of Rotation
var mass= 1.0;
var friction = 0.9;
var drag = false;
for (var i = 0; i < 5; i++){
var Background = new Sprite();
Background.name = "Background" + i;
Background.graphics.lineStyle(3, 0x663399, 1);
Background.graphics.beginFill(0x336699);
Background.graphics.drawRect(-20, -20, 40, 40);
Background.graphics.endFill();
Background.x = stage.stageWidth/2;
Background.y = stage.stageHeight/2;
Background.buttonMode = true;
Background.addEventListener(MouseEvent.MOUSE_OVER, onOver);
addChild(Background);
}
function onOver(e:MouseEvent):void{
var item = e.target;
item.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
}
function onDown(e:MouseEvent):void{
mx0 = stage.mouseX;
my0 = stage.mouseY;
drag = true;
var item = e.target;
item.addEventListener(Event.ENTER_FRAME, onMovement);
stage.addEventListener(MouseEvent.MOUSE_UP, onUp);
item.removeEventListener(MouseEvent.MOUSE_DOWN, onDown);
}
function onUp(e:MouseEvent):void{
drag = false;
var item = e.target;
item.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
stage.removeEventListener(MouseEvent.MOUSE_UP, onUp);
}
function onMovement(e:Event):void{
var instanceX = e.target.x;
var instanceY = e.target.y;
var instanceR = e.target.rotation;
if(drag == false)
{
instanceX += velocityX;
instanceY += velocityY;
}
else
{
// Set Current Mouse Position
var mx = stage.mouseX;
var my = stage.mouseY;
//Calculate Distance Traveled
var dmx = mx - mx0;
var dmy = my - my0;
// Set Previous Mouse Position
mx0 = mx;
my0 = my;
// Calculate Force
var fx = (dmx - velocityX) * mass;
var fy = (dmy - velocityY) * mass;
// Calculate Offset Between Mouse and Registration Point
var ax = mx - instanceX;
var ay = my - instanceY;
// Calculate Rotation from Offset
var t = fx * ay - fy * ax;
//Set Velocity = To Distance
velocityX = dmx;
velocityY = dmy;
// Set Rotation Angle
velocityR += t * .005;
// Set Instance = Instance + Velocity
instanceX += velocityX;
instanceY += velocityY;
//
var dx = mx - instanceX;
var dy = my - instanceY;
// Convert Degrees to Radians
var cos = Math.cos(velocityR * Math.PI / 180);
var sin = Math.sin(velocityR * Math.PI / 180);
// Calculate Offset Points After Rotation
var aax = dx - (cos * dx + sin * dy);
var aay = dy - (cos * dy - sin * dx);
// Set Instance Points after Rotation
instanceX += aax;
instanceY += aay;
}
instanceR -= velocityR;
// Bounce Off Edges
if (instanceX < 0) {
velocityX *= -1;
instanceX = 0;
}
if (instanceX > stage.stageWidth) {
velocityX *= -1;
instanceX = stage.stageWidth;
}
if (instanceY < 0) {
velocityY *= -1;
instanceY = 0;
}
if (instanceY > stage.stageHeight) {
velocityY *= -1;
instanceY = stage.stageHeight;
}
e.target.x = instanceX;
e.target.y = instanceY;
e.target.rotation = instanceR;
velocityX *= friction;
velocityY *= friction;
velocityR *= friction;
}
-
Code:
var mass=1.0;
var friction=0.9;
var drag=false;
var props:Dictionary=new Dictionary();
var currItem:Sprite;
for (var i = 0; i < 5; i++) {
var Background = new Sprite();
Background.name="Background"+i;
Background.graphics.lineStyle(3, 0x663399, 1);
Background.graphics.beginFill(0x336699);
Background.graphics.drawRect(-20, -20, 40, 40);
Background.graphics.endFill();
Background.x=stage.stageWidth/2;
Background.y=stage.stageHeight/2;
Background.buttonMode=true;
props[Background]={mx0:0,my0:0,vX:0,vY:0,vR:0,drag:false};
Background.addEventListener(MouseEvent.MOUSE_OVER, onOver);
addChild(Background);
}
function onOver(e:MouseEvent):void {
e.currentTarget.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
}
function onDown(e:MouseEvent):void {
currItem=e.currentTarget as Sprite;
props[currItem].mx0=stage.mouseX;
props[currItem].my0=stage.mouseY;
props[currItem].drag=true;
currItem.addEventListener(Event.ENTER_FRAME, onMovement);
stage.addEventListener(MouseEvent.MOUSE_UP, onUp);
currItem.removeEventListener(MouseEvent.MOUSE_DOWN, onDown);
}
function onUp(e:MouseEvent):void {
props[currItem].drag=false;
currItem.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
stage.removeEventListener(MouseEvent.MOUSE_UP, onUp);
}
function onMovement(e:Event):void {
var instanceX=e.currentTarget.x;
var instanceY=e.currentTarget.y;
var instanceR=e.currentTarget.rotation;
if (props[e.currentTarget].drag==false) {
instanceX+=props[e.currentTarget].vX;
instanceY+=props[e.currentTarget].vY;
} else {
// Set Current Mouse Position
var mx=stage.mouseX;
var my=stage.mouseY;
//Calculate Distance Traveled
var dmx=mx-props[e.currentTarget].mx0;
var dmy=my-props[e.currentTarget].my0;
// Set Previous Mouse Position
props[e.currentTarget].mx0=mx;
props[e.currentTarget].my0=my;
// Calculate Force
var fx = (dmx - props[e.currentTarget].vX) * mass;
var fy = (dmy - props[e.currentTarget].vY) * mass;
// Calculate Offset Between Mouse and Registration Point
var ax=mx-instanceX;
var ay=my-instanceY;
// Calculate Rotation from Offset
var t=fx*ay-fy*ax;
//Set Velocity = To Distance
props[e.currentTarget].vX=dmx;
props[e.currentTarget].vY=dmy;
// Set Rotation Angle
props[e.currentTarget].vR+=t*.005;
// Set Instance = Instance + Velocity
instanceX+=props[e.currentTarget].vX;
instanceY+=props[e.currentTarget].vY;
//
var dx=mx-instanceX;
var dy=my-instanceY;
// Convert Degrees to Radians
var cos=Math.cos(props[e.currentTarget].vR*Math.PI/180);
var sin=Math.sin(props[e.currentTarget].vR*Math.PI/180);
// Calculate Offset Points After Rotation
var aax = dx - (cos * dx + sin * dy);
var aay = dy - (cos * dy - sin * dx);
// Set Instance Points after Rotation
instanceX+=aax;
instanceY+=aay;
}
instanceR-=props[e.currentTarget].vR;
// Bounce Off Edges
if (instanceX<0) {
props[e.currentTarget].vX*=-1;
instanceX=0;
}
if (instanceX>stage.stageWidth) {
props[e.currentTarget].vX*=-1;
instanceX=stage.stageWidth;
}
if (instanceY<0) {
props[e.currentTarget].vY*=-1;
instanceY=0;
}
if (instanceY>stage.stageHeight) {
props[e.currentTarget].vY*=-1;
instanceY=stage.stageHeight;
}
e.currentTarget.x=instanceX;
e.currentTarget.y=instanceY;
e.currentTarget.rotation=instanceR;
props[e.currentTarget].vX*=friction;
props[e.currentTarget].vY*=friction;
props[e.currentTarget].vR*=friction;
}
-
Why couldn't you just have a variable called inMotion or something that when true you can't select anything else until inMotion equals false?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|