Copy link to clipboard
Copied
Hi! I am quite new to as3 and i am trying to figure out how to make an object to appear like it's being catapulted from a slingshot.
Here is the code I've done so far. The problem is, my objects starts moving from the start and not only when it is released... How can I make it move like it's being thrown when I release the mouse?
stop();
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.display.Shape;
import flash.display.MovieClip;
import flash.sensors.Accelerometer;
import flash.events.AccelerometerEvent;
var bird1Start: Object = (bird1.x , bird1.y);
var bird2Start: Object = (bird2.x , bird2.y);
var bird3Start: Object = (bird3.x , bird3.y);
var gravity = 0.1;
var angle1: Number = 0;
var angle2: Number = 0;
var radius: Number = 1;
var elasticCoefficient: Number = 0.0015;
var released : Boolean = true;
var forced: Boolean = false; //weather or not it is being pulled down between sticks
var accX: Number =0;
var accY: Number = 0;
var velX: Number =0;
var velY: Number =0;
var level : Number =1;
lvl.text = String (level);
var hitwood1: Boolean = false;
var hitwood2: Boolean = false;
var hitwood3: Boolean = false;
var hitwood4: Boolean = false;
var hitwood5: Boolean = false;
var hitwood6: Boolean = false;
var elastic:MovieClip = new MovieClip();
addChild(elastic);
function resetPlay(){
velX = 0;
velY = 0;
accX =0;
accY = gravity;
forced = false;
bird1.x = bird1Start.x;
bird1.y = bird1Start.y;
bird2.x = bird2Start.x;
bird2.y = bird2Start.y;
bird3.x = bird3Start.x;
bird3.y = bird3Start.y;
}
bird1.addEventListener (MouseEvent.MOUSE_DOWN, bird1clicked)
function bird1clicked(event:MouseEvent){
bird1.startDrag();
released= false;
}
bird1.addEventListener (MouseEvent.MOUSE_UP, bird1released)
function bird1released(event:MouseEvent){
bird1.stopDrag();
released = true;
}
addEventListener (Event.ENTER_FRAME, doConstantly);
function doConstantly(event:Event): void {
// if i take this statement out, the object moves only dragged by mouse, it's not thrown
if (released){
bird1.x+=velX;
bird1.y+=velY;
bird1.rotation+= velX;
}
// drawing elastic
elastic.graphics.clear();
elastic.graphics.lineStyle(1, 0x000000);
if(bird1.x<point2.x){ //if the bird is on the left side of the slingshot
forced = true;
var x1:Number =bird1.x -point1.x;
var y1:Number =bird1.y -point1.y;
var x2:Number =point2.x - bird1.x;
var y2:Number =point2.y - bird1.y;
var distance1: Number = Math.sqrt(x1*x1+y1*y1);
var distance2: Number = Math.sqrt(x2*x2+y2*y2);
angle1= Math.atan2(y1,x1);
angle2= Math.atan2(y2,x2);
var xOffset: Number = Math.cos (angle1+Math.PI/2)*radius;
var yOffset: Number = Math.sin (angle1+Math.PI/2)*radius;
var xOffset2: Number = Math.cos (angle2+Math.PI/2)*radius;
var yOffset2: Number = Math.sin (angle2+Math.PI/2)*radius;
angle1 += Math.sin(radius/distance1);
angle2 += Math.sin(radius/distance2)*-1;
elastic.graphics.moveTo(point1.x, point1.y);
elastic.graphics.lineTo(bird1.x+5, bird1.y+20);
elastic.graphics.moveTo(point2.x, point2.y);
elastic.graphics.lineTo(bird1.x+5, bird1.y+20);
} else { //the bird is on the right side of the slingshot
forced = false;
elastic.graphics.moveTo(point1.x, point1.y);
elastic.graphics.lineTo(point2.x, point2.y);
}
if (released === true&& forced === true){
accX+= distance1*Math.sin(angle2)*elasticCoefficient;
accY+= -distance1*Math.cos(angle1)*elasticCoefficient;
accX+= distance2*Math.sin(angle1)*elasticCoefficient;
accY+= -distance2*Math.cos(angle2)*elasticCoefficient;
}
if (released){
velX += accX;
velX+=accY;
}
}
Not sure what is being catapuylted but it looks like a bird is. In any case, the activity appears to be set off when the released variable is set to be true, and you set it to be true immediately.
var released : Boolean = true;
Copy link to clipboard
Copied
Not sure what is being catapuylted but it looks like a bird is. In any case, the activity appears to be set off when the released variable is set to be true, and you set it to be true immediately.
var released : Boolean = true;
Copy link to clipboard
Copied
Omg you’re right, I can’t believe I didn’t see that. Thank you! I do have another problem though... when the bird is being catapulted it doesn’t actually follow the direction is being thrown, but it just bounces like a ball.. do you have any idea on how to fix that?
Copy link to clipboard
Copied
Could it be that you are coding in a two dimensional plane whereas you wish to have the object travel as though it is in three dimensional space? Maybe you need to include the z-axis as well?
Copy link to clipboard
Copied
Not really.. I am trying to create something similar with angry birds game... so I don’t think It’s in a 3D space...
Copy link to clipboard
Copied
Never played Angry Birds... only ever heard of it. If things are only moving up and down then you need to focus on what makes the x position change.
Copy link to clipboard
Copied
The idea is that I want my bird to be controlled by a Catapult. between the sticks of the catapult I have made the string which follows the position of the bird when I put it on the string and it does look like it’s pulled by the bird. What I am looking to do is when i release the bird, the bird to look like it’s being catapulted by the string in the direction it is being catapulted. For example, if the bird is being catapulted from the left-down side of the catapult, the bird is being thrown with more force in the upper-right side of the screen, if the bird is being catapulted from top-left of the screen, the bird is being catapulted to the Lower-right side of the screen... if it makes sense...
Find more inspiration, events, and resources on the new Adobe Community
Explore Now