As3 remove current clip if playing - actionscript-3

I have buttons that add movie clips to stage and wont go away when other button is clicked to add another. I've seen similar issues posted but none of the answers worked for me.
btnBlue.addEventListener(MouseEvent.CLICK, fl_MouseOverHandler1);
function fl_MouseOverHandler1(evt: MouseEvent): void {
var ballBlue: BallBlue = new BallBlue;
addChild(ballBlue);
addChildAt(ballBlue, 0);
ballBlue.x = 100; //sets the x position
ballBlue.y = 100; //sets the y position}
function onClick(evt:MouseEvent):void { */to remove clip if one is playing
removeChildAt(0)
}
}
btnRed.addEventListener(MouseEvent.CLICK, fl_MouseOverHandler2);
function fl_MouseOverHandler2(evt: MouseEvent): void {
var ballRed: BallRed = new BallRed;
addChild(ballRed);
addChildAt(ballRed, 0);
ballRed.x = 100; //sets the x position
ballRed.y = 100; //sets the y position}
parent.BallBlue.removeMovieClip();
function onClick(evt:MouseEvent):void { */to remove clip if one is playing
removeChildAt(0)
}
}

btnBlue.addEventListener(MouseEvent.CLICK, btnBlue_clickHandler);
btnRed.addEventListener(MouseEvent.CLICK, btnRed_clickHandler);
var ballBlue: BallBlue = new BallBlue();
var ballRed: BallRed = new BallRed();
function btnBlue_clickHandler(evt:MouseEvent):void {
removeAllBalls();
addChild(ballBlue);
ballBlue.x = 100; //sets the x position
ballBlue.y = 100; //sets the y position
}
function btnRed_clickHandler(evt:MouseEvent):void {
removeAllBalls();
addChild(ballRed);
ballRed.x = 100; //sets the x position
ballRed.y = 100; //sets the y position
}
function removeAllBalls():void {
if (contains(ballBlue))
{
removeChild(ballBlue);
}
if (contains(ballRed))
{
removeChild(ballRed);
}
}

Related

move object within given perimeter only

I'm creating a game(not exactly) for kids. It's to illustrate we can make infinite triangles with fixed length of thread, pin board and three pins. Like below:
Students can move any pin. Doing so will create new triangle but the perimeter of the triangle should be of given length. I used the following code:
var xMax:Number = drawBoard.x + drawBoard.width;
var xMin:Number = drawBoard.x;
var yMax:Number = drawBoard.y + drawBoard.height;
var yMin:Number = drawBoard.y;
var isDragging1:Boolean = false;
var isDragging2:Boolean = false;
var isDragging3:Boolean = false;
drawBoard.x = xMin;
drawBoard.y = yMin;
var lines:Shape = new Shape();
addChild(lines);
stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseUpdate);
stage.addEventListener(MouseEvent.MOUSE_UP, stopDragging);
pin1.addEventListener(MouseEvent.MOUSE_DOWN, startDragging1);
pin2.addEventListener(MouseEvent.MOUSE_DOWN, startDragging2);
pin3.addEventListener(MouseEvent.MOUSE_DOWN, startDragging3);
function mouseUpdate(evt:MouseEvent):void
{
if (isDragging1)
{
pin1.x = validX(evt.stageX);
pin1.y = validY(evt.stageY);
lines.graphics.clear();
drawLines();
}
if (isDragging2)
{
pin2.x = validX(evt.stageX);
pin2.y = validY(evt.stageY);
lines.graphics.clear();
drawLines();
}
if (isDragging3)
{
pin3.x = validX(evt.stageX);
pin3.y = validY(evt.stageY);
lines.graphics.clear();
drawLines();
}
evt.updateAfterEvent();
}
function validX(thisX:Number):Number
{
if (thisX < xMin)
{
return (xMin);
}
if (thisX > (xMax))
{
return (xMax);
}
return (thisX);
}
function validY(thisY:Number):Number
{
if (thisY < yMin)
{
return (yMin);
}
if (thisY > (yMax))
{
return (yMax);
}
return thisY;
}
function drawLines():void
{
lines.graphics.lineStyle(1, 0xFF0000);
lines.graphics.moveTo(pin1.x, pin1.y);
lines.graphics.lineTo(pin2.x, pin2.y);
lines.graphics.lineTo(pin3.x, pin3.y);
lines.graphics.lineTo(pin1.x, pin1.y);
lines.filters = [new DropShadowFilter()];
}
function startDragging1(evt:MouseEvent):void
{
isDragging1 = true;
}
function startDragging2(evt:MouseEvent):void
{
isDragging2 = true;
}
function startDragging3(evt:MouseEvent):void
{
isDragging3 = true;
}
function stopDragging(evt:MouseEvent):void
{
isDragging1 = false;
isDragging2 = false;
isDragging3 = false;
}
function DistanceTwoPoints(x1:Number, x2:Number, y1:Number, y2:Number):Number
{
var dx:Number = x1 - x2;
var dy:Number = y1 - y2;
return Math.sqrt(dx * dx + dy * dy);
}
It does the half part. I can move any pin and it will create triangle. But the perimeter is different everytime. I tried to restrict it by calculating the distance between three sides. The problem with that is, I can move pin anywhere which makes it difficult to get exact perimeter. As a result, it makes it difficult to move pins smoothly. So, my question is, is it possible to move the pins only to valid location instead of moving along mouse? Any help would be appreciated.

rotating sprite from it's current angle as3

i created an object that is rotated with the mouse, however each time after rotating, when mouse is clicked again the object "jump" right to it's new angle i would like that the object will continue to rotate from it's current location no matter where the mouse being clicked.
this is my code that rotates the object.
public function _mouseclick(me:MouseEvent):void
{
addEventListener(Event.ENTER_FRAME, UpdateGame);
IsButtonClicked = true;
}
public function UpdateGame(e:Event):void
{
if (IsButtonClicked)
{
var dist_Y:Number = mouseY - PlayerSprite.y ;
var dist_X:Number = mouseX - PlayerSprite.x ;
var angle:Number = Math.atan2(dist_Y, dist_X);
var degrees:Number = angle * 180/ Math.PI;
PlayerSprite.rotation = degrees;
}
}
how can i reset the angle to the current sprite angle and prevent that "jump"?
It looks like maybe you are trying to make some sort of knob or dial. Finding the angle between the mouse and sprite and applying it the way you are, will always face that direction. I think you are looking for something a little more simple.
First I would set up 2 variables, one to be the amount to increment the rotation, the other to store the mouse position.
var increment:int = 5;
var mousePos:Point;
Then in your mouse click function:
mousePos = new Point( mouseX, mouseY );
Lastly in your UpdateGame():
private function UpdateGame( e:Event ):void {
if ( IsButtonClicked ) {
var increment:int = 5;
if ( mousePos.y < mouseY ) {
spr.rotation += increment;
}
else {
spr.rotation -= increment;
}
mousePos = new Point( mouseX, mouseY );
}
}
That seems to be more the effect you are looking for. In this case when you move the mouse up, the sprite rotates counter clockwise. Moving the mouse down, rotates it clockwise.
If you want to change the rotation smoothly, you want to base it on the change from last rotation as the delta.
The delta is based on the initial rotation from mouse click relative to the current rotation.
current_rotation += (atan2 - last_rotation);
last_rotation = atan2;
And the full pseudo example.
private var current_rotation:Number;
private var last_rotation:Number;
private var mouse_down:Boolean;
public function MouseClick(e:MouseEvent):void
{
var dX:Number = Mouse.X - 400;
var dY:Number = Mouse.Y - 300;
var atan2:Number = Math.atan2(dY, dX);
last_rotation = atan2;
mouse_down = true;
}
public function UpdateGame(e:Event):void
{
if (mouse_down)
{
var dX:Number = Mouse.X - 400;
var dY:Number = Mouse.Y - 300;
var atan2:Number = Math.atan2(dY, dX);
current_rotation += (atan2 - last_rotation);
last_rotation = atan2;
}
}

How can I turn an AS2 snow effect into a AS3 snow effect?

So I manage to find a snow effect that I like and wanted to use it but I realized it was in AS2 and I need it to be in AS3. Since there isn't a small difference between AS2 and AS3 I'm here to find some in these matter.
As you can see in the fla provided I also want to control the wind and speed by buttons.
Here is a link to the AS2 snow effect: http://www.freeactionscript.com/download/realistic-snow-fall-snowflake-effect.zip
This is the code in AS2:
//settings
var speed:Number = 2;
var wind:Number = -2;
var movieWidth:Number = 550;
var movieHeight:Number = 400;
createSnow(_root, 100);
function createSnow(container:MovieClip, numberOfFlakes:Number):Void
{
//run a for loop based on numberOfFlakes
for (var i = 0; i < numberOfFlakes; i++)
{
//set temporary variable and attach snowflake to it from the library
var tempFlake:MovieClip = container.attachMovie("snow_mc", "snow"+container.getNextHighestDepth(), container.getNextHighestDepth());
//variables that will modify the falling snow
tempFlake.r = 1+Math.random()*speed;
tempFlake.k = -Math.PI+Math.random()*Math.PI;
tempFlake.rad = 0;
//giving each snowflake unique characteristics
var randomScale:Number = random(50)+50;
tempFlake._xscale = randomScale;
tempFlake._yscale = randomScale
tempFlake._alpha = random(100)+50;
tempFlake._x = random(movieWidth);
tempFlake._y = random(movieHeight);
//give the flake an onEnterFrame function to constantly update its properties
tempFlake.onEnterFrame = function()
{
//update flake position
this.rad += (this.k / 180) * Math.PI;
this._x -= Math.cos(this.rad)+wind;
this._y += speed;
//if flake out of bounds, move to other side of screen
if (this._y >= movieHeight) {
this._y = -5;
}
if (this._x >= movieWidth)
{
this._x = 1
}
if (this._x <= 0)
{
this._x = movieWidth - 1;
}
}
}
}
//buttons
//wind
left_btn.onRelease = function()
{
wind = 2;
}
none_btn.onRelease = function()
{
wind = 0;
}
right_btn.onRelease = function()
{
wind = -2;
}
//speed
slow_btn.onRelease = function()
{
speed = .5;
}
normal_btn.onRelease = function()
{
speed = 1
}
fast_btn.onRelease = function()
{
speed = 3
}
It's going to be really quite similar.
The first thing would be, instead of:
var tempFlake:MovieClip = container.attachMovie("snow_mc", "snow"+...
you want something like:
var tempFlake = new snow_mc();
container.addChild(tempFlake);
Then convert all the property names such as _x etc to their AS3 equivalents (no underscore, scaleX in place f _xscale etc), Math.random() * 50 in place of random(50).
Replace all onRelease with addEventListener(MouseEvent.CLICK, function() {})
Finally instead of tempFlake.onEnterFrame you'll need one frame loop something like:
function onFrame(event: Event): void {
foreach(var child: MovieClip in container) {
child.rad += ... etc
}
}
addEventListener(Event.ENTER_FRAME, onFrame);
These steps should be sufficient to get it working as AS3. Once it's running, you could go further to make it more AS3 by creating a SnowFlake class that encapsulates all the properties and updates for one snowflake.

Dragging with Actionscript 3.0, like moving camera in RTS games

I am trying to build a RTS game with Flash and doing some basic testing. I come across this site teaching me dragging objects. I am modified the code to simulate moving the game world of the game while clicking on it. The center circle is the focus point / center of camera. The rectangle board represents the game world.
I tried to change the function boardMove to click and move according to mouseX and mouseY. But every time I click, the mouseX and mouseY becomes the center of the board, which is not what I wanted. I want to make it relative to the mouse position, but I could only make the board flickering, or moves with its top left corner.
Any suggestions would be appreciated.
// Part 1 -- Setting up the objects
var board:Sprite = new Sprite();
var myPoint:Sprite = new Sprite();
var stageWidth = 550;
var stageHeight = 400;
var boardWidth = 400;
var boardHeight = 300;
var pointWidth = 10;
this.addChild(board);
this.addChild(myPoint);
board.graphics.lineStyle(1,0);
board.graphics.beginFill(0xCCCCCC);
board.graphics.drawRect(0,0,boardWidth,boardHeight);
board.graphics.endFill();
board.x = (stageWidth - boardWidth) / 2;
board.y = (stageHeight - boardHeight) / 2;
myPoint.graphics.lineStyle(1,0);
myPoint.graphics.beginFill(0x0000FF,0.7);
myPoint.graphics.drawCircle(0,0,pointWidth);
myPoint.graphics.endFill();
myPoint.x = (stageWidth - pointWidth) / 2;
myPoint.y = (stageHeight - pointWidth) / 2;
// Part 2 -- Add drag-and-drop functionality - Better Attempt
stage.addEventListener(MouseEvent.MOUSE_DOWN, startMove);
function startMove(evt:MouseEvent):void {
stage.addEventListener(MouseEvent.MOUSE_MOVE, boardMove);
}
// Revised definition of pointMove in Part II of our script
function boardMove(e:MouseEvent):void {
board.x = checkEdgeX(board.mouseX);
board.y = checkEdgeY(board.mouseY);
e.updateAfterEvent();
}
stage.addEventListener(MouseEvent.MOUSE_UP, stopMove);
function stopMove(e:MouseEvent):void {
stage.removeEventListener(MouseEvent.MOUSE_MOVE, boardMove);
}
// Part III -- Check for boundaries
function checkEdgeX(inX:Number):Number {
var x = stageWidth / 2 - boardWidth;
if (inX < x) {
return x;
}
x = stageWidth / 2;
if (inX > x) {
return x;
}
return inX;
}
function checkEdgeY(inY:Number):Number {
var y = stageHeight / 2 - boardHeight;
if (inY < y) {
return y;
}
y = stageHeight / 2;
if (inY > y) {
return y;
}
return inY;
}
One option is to determine the relative movement of the mouse and move the board accordingly; something like:
private Point lastPosition;
function startMove(...) {
lastPosition = null;
...
}
function boardMove(e:MouseEvent):void {
Point position = new Point(stageX, stageY);
if (lastPosition != null) {
Point delta = position.subtract(lastPosition);
board.x += delta.x; // NOTE: also try -= instead of +=
board.y += delta.y; // NOTE: also try -= instead of +=
e.updateAfterEvent();
}
lastPosition = position;
}

Generate random falling objects in AS3

I want to create a set of random objects to fall down the stage in a loop.
So far I have created a test object to fall at a random x coordinate. I am having trouble working out how to loop the falling function so multiple instances of the object continuously fall.
var randomX:Number = Math.random() * 800;
test_mc.x = randomX;
test_mc.y = 0;
var speed:Number = 10;
test_mc.addEventListener(Event.ENTER_FRAME, moveDown);
function moveDown(e:Event):void
{
e.target.y += speed;
if(e.target.y >= 480)
{
test_mc.removeEventListener(Event.ENTER_FRAME, moveDown);
}
}
refer my a falling snow effect code.
The starting position of the snow is all random, almost same effect that real snow falling circumstance. If you run You'll be amazed.
The Snow is My Custom MovieClip (white circle shape, width 15, height 15)
here is my demo: SnowEffect
here is my source: SnowEffect Down
this.addEventListener( Event.ENTER_FRAME, onEnter );
function onEnter( e: Event ):void {
var s: Snow = new Snow();
s.x=550*Math.random();
s.y=-20;
s.width=s.height=1+9*Math.random();// 1 ~ 9
s.xSpeed=-2+4*Math.random();// -2 ~ 2
s.ySpeed=1+4*Math.random();// 1 ~ 5
s.at = -0.001 -0.001*Math.random();
s.vt = 0;
this.addChild( s );
s.addEventListener( Event.ENTER_FRAME, onSnowEnter );
}
function onSnowEnter( e: Event ):void {
var s:Snow=e.currentTarget as Snow;
s.x+=s.xSpeed;
s.y+=s.ySpeed;
if (s.y>=480) {
s.addEventListener( Event.ENTER_FRAME, onMeltingEnter );
}
}
function onMeltingEnter( e: Event ): void {
var s:Snow=e.currentTarget as Snow;
this.addChild( s );
s.removeEventListener( Event.ENTER_FRAME, onSnowEnter );
s.vt += s.at;
s.alpha += s.vt;
if ( s.alpha <=0){
s.removeEventListener( Event.ENTER_FRAME, onMeltingEnter );
this.removeChild( s );
}
}
Create a bunch of objects, add them to an array and then loop through the array:
var numOfObjects:int = 10;
var fallingObjectArray:Array = [];
var speed:Number = 10;
// Add 10 falling objects to the display and to the array
for(var i:int = 0; i < numOfObjects; i++) {
var fallingObject:Sprite = new Sprite();
fallingObject.graphics.beginFill(0xFF0000);
fallingObject.graphics.drawCircle(0, 0, 15);
fallingObject.graphics.endFill();
addChild(fallingObject);
fallingObject.x = Math.random() * stage.stageWidth;
fallingObjectArray.push(fallingObject);
}
addEventListener(Event.ENTER_FRAME, moveDown);
function moveDown(e:Event):void
{
// Go through all the objects in the array and move them down
for each(var fallingObject in fallingObjectArray) {
fallingObject.y += speed;
// If the object is past the screen height, remove it from display and array
if(fallingObject.y+fallingObject.height >= stage.stageHeight) {
removeChild(fallingObject);
fallingObjectArray.splice(fallingObjectArray.indexOf(fallingObject), 1);
}
}
// Once all objects have fallen off the screen, remove the listener
if(fallingObjectArray.length <= 0) {
removeEventListener(Event.ENTER_FRAME, moveDown);
}
}
The code above is just using red circles - you will have to use whatever image you have instead (unless you like red circles...).