Can some one tell me why this code is moving the box left but not right? AS3 - actionscript-3

Very simple code, box moves to the left slowly.
addEventListener(Event.ENTER_FRAME, framecheck);
function framecheck(event:Event):void
{
box.x +=1.13;
box.x -=1.2;
}
...and when we reverse the values, you'd think the box would move to the right slowly? but it doesn't?
addEventListener(Event.ENTER_FRAME, framecheck);
function framecheck(event:Event):void
{
box.x -=1.13;
box.x +=1.2;
}
What is wrong with the code, how do i fix this?

Related

How to use event Dispatch in actionscript 3.0

I tried using the event dispatch but unable to get clear view of this one, so tell me if anyone know this.
This is Sample Code but i can't understand could you explain elaborate.
var ball:Shape = new Shape();
ball.graphics.beginFill(0xFF0000);
ball.graphics.drawCircle(0, 0, 30);
ball.graphics.endFill();
addChild(ball);
stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveListener);
addEventListener("myCustomEvent", myCustomEventListener);
function mouseMoveListener(event:MouseEvent):void
{
dispatchEvent(new Event("myCustomEvent"));
}
function myCustomEventListener(event:Event):void
{
ball.x = stage.mouseX;
ball.y = stage.mouseY;
}
Think about stage as a big box where magic happens. Stage knows when you resize it, when you press keyboard buttons or when you move mouse on top of it. It does extend EventDispatcher which means it can broadcast stuff (and it does!). You usually don't pay attention to it, but in this particular piece of code you have:
stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveListener);
function mouseMoveListener(event:MouseEvent):void
{
dispatchEvent(new Event("myCustomEvent"));
}
What's going on in here? You're curious about MOUSE_MOVE event and that's why you add listener. Now every time you move your mouse over the stage, stage is going to broadcast (dispatch) event (like : "HEY! Mouse just moved!"). And every time that happens your mouseMoveListener would be called. You can add trace(stage.mouseX, stage.mouseY); inside it and it would trace your mouse position as you move it. You could also move this code
ball.x = stage.mouseX;
ball.y = stage.mouseY;
to that function and your ball would follow mouse cursor.
However what you're doing is:
dispatchEvent(new Event("myCustomEvent"));
Which basically means that you're broadcasting (dispatching) an event now. What event? "myCustomEvent". Now anyone who is interested might listen for it. And somebody is indeed:
addEventListener("myCustomEvent", myCustomEventListener);
function myCustomEventListener(event:Event):void
{
ball.x = stage.mouseX;
ball.y = stage.mouseY;
}
So you're basically listening for this even in the same class and when this even it being dispatched you execute myCustomEventListener function.
Hope that's gonna make it a bit more clearer for you :)

Actionscript 3.0 - Making a sprite move after it has collided with another

I'm trying to code a movie clip object to move after it has been "hit" with another object. I can make it move like this:
if (bat.hitTestObject(ball)
{
bat.x += xMovement;
}
This only makes it move by xMovement which in this case is 5 when it has hit but I would like it to continue to move afterwards. Thanks for the help
To "keep moving" you need to increment over time, for example at each frame using enterFrame or at an arbitrary interval using a Timer.
For example:
if (bat.hitTestObject(ball)) {
addEventListener(Event.ENTER_FRAME, moveBat);
}
function moveBat(e:Event):void {
bat.x += xMovement;
}
To stop moving the bat at any point, remove the enterFrame handler:
removeEventListener(Event.ENTER_FRAME, moveBat);

How to stop an effect from one frame to continue in the next?

I have made a program in which small green rectangles (or Greeny here) are periodically generated at the bottom of the screen and inside the movie clip symbol, I have made a classic tween such that it moves to the top of the screen. I have also declared a variable (t) that increments itself periodically (I am running it at 24 fps). When the value variable, t reaches or exceeds 96, it moves to the next frame. The problem however is that even in the next frame, the generation of these small green rectangles do not stop. Please do excuse me if I have asked the question wrongly. By the way the code on frame 2 is just stop();. Here is the code for frame 1-
var c:int;
var t:int = 0;
var s:int = 8;
function eFrame(event:Event):void
{
t++;
if (t%s == 0)
{
var i:Greeny = new Greeny ;
i.x = Math.random() * 550;
i.y = 400;
stage.addChild(i);
}
if (t > 96) {
nextFrame();
}
}
this.addEventListener(Event.ENTER_FRAME, eFrame);
stop();
EDIT - Here is the link for the file - http://www.mediafire.com/download/crjh2fubcbnx3l5/Retro.fla
you have to remove your your event listener on frame 2. Try this (on frame2 or in frame 1 when your condition t>96 is reached):
this.removeEventListener(Event.ENTER_FRAME, eFrame);
If you want to stop the generation of green rectangles, you should remove the listener of Event.Enter_Frame, or do something to stop it in function eFrame.
If you want to make all rectangles not visible, you should set theirs "visible" attribute false or remove them from stage .

stage.swapChildren(MClip1, Mclip2); not working

i have made a drag and match game where i have a rectangle circle movieclip ..when rectangle/or circle hit a specific box rectangle will always be behind circle and circle will always be over retangle,if i drag rectangle first then circle its ok..but if i drag circle first then rectangle ..circle always takes its position behind ractengle i used stage.swapChildren(ccircle, crect) but its not working ...here is my code
crect.addEventListener(MouseEvent.MOUSE_DOWN, item_onMouseDown1);
function item_onMouseDown1(event:MouseEvent):void
{
crect = MovieClip(event.target);
startX1 = crect.x;
startY1 = crect.y;
crect.startDrag();
setChildIndex(crect, this.numChildren-1);
stage.addEventListener(MouseEvent.MOUSE_UP, stage_onMouseUp1);
}
function stage_onMouseUp1(event:MouseEvent):void {
stage.removeEventListener(MouseEvent.MOUSE_UP, stage_onMouseUp1);
crect.stopDrag();
if(crect.hitTestObject(box3))
{
TweenMax.to(crect, 0.5, {x:hit2X, y:hit2Y,height:height2Y, width:weight2X, ease:Cubic.easeOut});
//crect.mouseEnabled=false;
}
else
{
TweenMax.to(crect, 0.5, {x:startX1, y:startY1, ease:Bounce.easeOut});
}
}
ccircle.buttonMode=true;
ccircle.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown1);
function onMouseDown1(event:MouseEvent):void
{
ccircle = MovieClip(event.target);
startX1 = ccircle.x;
startY1 = ccircle.y;
ccircle.startDrag();
setChildIndex(ccircle, this.numChildren-1);
stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp1);
}
function onMouseUp1(event:MouseEvent):void {
stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp1);
ccircle.stopDrag();
if(ccircle.hitTestObject(box3))
{
TweenMax.to(ccircle, 0.5, {x:hit1X, y:hit1Y,height:height1Y, width:weight1X, ease:Cubic.easeOut});
//stage.swapChildren(ccircle, crect);
//setChildIndex(ccircle, this.numChildren-1);
//ccircle.mouseEnabled=false;
}
else
{
TweenMax.to(ccircle, 0.5, {x:startX1, y:startY1, ease:Bounce.easeOut});
}
}
You can't change ccircle index to -1. Only 0, 1, 2, ...
stage.setChildIndex(ccircle, -1);
You should write:
if (getChildIndex(ccircle) < getChildIndex(crect))
swapChildren(ccircle, crect);
It means: If ccircle is under crect then put ccircle on top.
You can just use
removeChild(crect);
addChild(crect);
That will always put crect on top. Of course if you need ccircle on top, do the same to it.
i simply set setChildIndex(crect, this.numChildren-2);
and setChildIndex(crect, this.numChildren-2); and its working perfect,because its not for setting circle on top only.while dragging i have to put dragged object on top of other objects..
I also found some info you can read on
Gary Rosenzweig website that I been studying myself on the display list in flash.
The site is http://flashgameu.com/ and scroll down an look for "understanding the display list."
It explains swapping children by name and also by location in the display list.
Watching his pod cast will help you understand the options using the display list with swapping children, addChild,removeChild and childIndex.
Its also free and a good site with some information on how to do things with flash and as3 programming.

how to check, whether user is dragging the object rightside or leftside in actionscript 3

I am creating a flash based application, where user can change a rectangle shapes, width and height using mouse drag. here is a quick prototype image.
let me explain briefly: In the image you can see, i am having a tiny red rectangle, which one now sitting is starting position and user can drag that only 100px to right side. The idea is when, user is dragging that to right, i want to rectangle also expand right side with that, like a flexible box. if he is dragging back then it will return with that.
so, the doubt is: how will check, whether user is dragging right side or left side. so based on that we can update the rectangle width.
Here is the code :
import flash.geom.Rectangle;
import flash.events.MouseEvent;
var horizRect:Rectangle = new Rectangle(scrollPathHoriz.x, scrollPathHoriz.y, 100, 0);
var horizCount:Number;
//event listener for the anchor point.
scrollHoriz.addEventListener(MouseEvent.MOUSE_DOWN, dragScroller);
stage.addEventListener(MouseEvent.MOUSE_UP, dropScroller);
//mouse down and mouse up event handler.
function dragScroller(evt:MouseEvent):void {
horizCount= scrollHoriz.x;
scrollHoriz.startDrag(false, horizRect);
scrollHoriz.addEventListener(MouseEvent.MOUSE_MOVE, calculateHorizPixel);
}
function dropScroller(evt:MouseEvent):void {
scrollHoriz.stopDrag();
scrollHoriz.removeEventListener(MouseEvent.MOUSE_MOVE, calculateHorizPixel);
}
function calculateHorizPixel(evt:MouseEvent):void {
horizCount ++;
trace(horizCount);
}
Since you are already saving the starting x position,
You simply need the difference between starting position & current position :
function calculateHorizPixel(evt:MouseEvent):void {
var dx = scrollHoriz.x - horizCount;
trace(dx);
}
A negative value of dx indicates that scrollHoriz moved left, else right.
Or try this:
import flash.events.MouseEvent;
stage.addEventListener(MouseEvent.MOUSE_MOVE, moveMouse);
function moveMouse(event:MouseEvent):void
{
var dx:Number = mouseX - stage.stageWidth/2;
trace(dx);
}