AS3 When symbol is in a certain position - actionscript-3

I'm using Flash Professional CS5.5 and I need to make an app where there is a ball (symbol) that moves using the accelerometer and I want that, when the ball A coordinates reach this coordinates B I go to frame 2 (gotoAndPlay(2)). I have to find the ball coord first, right?
How do I make this?
Here is the code I've now
c_ball.addEventListener(MouseEvent.MOUSE_DOWN, fl_ClickToDrag);
function fl_ClickToDrag(event:MouseEvent):void{
c_ball.startDrag();}
stage.addEventListener(MouseEvent.MOUSE_UP, fl_ReleaseToDrop);
function fl_ReleaseToDrop(event:MouseEvent):void{
c_ball.stopDrag();}
would it work if, after retriving the coordinates?
function f_level (e) if (c_ball.x==100 && c_ball.y==100) {
gotoAndStop(2);}

Use Collision detection creating a targetarea and test it with your ball - object if wanted onEnterframe or when every you thing its time:
private function test():void{
if( ball.hitTestObject(testarea) ){
// here goes next frame command ;)
}
}

I would add an enter frame event listener, and check the coordinates of c_ball there.
stage.addEventListener(Event.ENTER_FRAME, siteLoop);
public function siteLoop(event:Event)
{
if ((c_ball.x > 99.9) && (c_ball.y > 99.9)){
gotoAndStop(2);
}
}

Related

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 detect what class an instance is from in flash(as3)

I am relatively new to Flash and I am trying to make a little game.
For that I need to detect, if the player clicked on a plane or a bird.
I am spawning them with addChild and the name of each instance is generated.
The eventlistener is attached to the instance.
I tried detecting it like that, but it doesn't seam to work.
It detects the clicking (it prints out the shot: instance but not the trace commands in the if), but not was was clicked on.
function shoot(e: MouseEvent): void {
trace("shot: "+ e.target.name);
if (e.target is Plane) {
trace("shot plane");
e.target.parent.removeChild(e.target);
gotoAndStop(3);
}
if (e.target == Bird) {
trace("shot bird");
score += 1;
e.target.parent.removeChild();
}
}
Does anybody have a tip?
Try using e.currentTarget rather than e.target.
if (e.currentTarget is Plane) {
...
}
if (e.currentTarget is Bird) {
...
}
The current target of an event is a reference to the item you added the event listener to. The target, on the other hand, is the item actually clicked (which could be the same as current target, or a descendant/child object of it)
You can use getQualifiedClassName to check the object type:
trace(flash.utils.getQualifiedClassName(e.currentTarget));

Play only a certain range of frames

I have a combobox on the stage and changing its value I want to play only a certain range of frames:
stop();
combo01.addEventListener(Event.CHANGE, change);
function change(event:Event):void{
if (combo01.selectedItem.label == "BAL"){
gotoAndPlay(50);
if (currentFrame == 99) {stop();}
}
}
The game is not stopped but returned to frame 1.
You want your current frame check to happen when frame 99 is reached, not when change() is called. One way you can do this is to add a listener to check each frame as it is entered by the timeline until it reaches your desired frame:
addEventListener(Event.ENTER_FRAME, checkFrame);
function checkFrame(e:Event):void {
if(currentFrame == 99){
stop();
removeEventListener(Event.ENTER_FRAME, checkFrame);
}
}
Another way is to use the undocumented (but long supported) addFrameScript(): actionscript3 whats the point of addFrameScript
You could also just put a conditional stop() on frame 99, such as if (stopFrame == 99) stop(), then simply set stopFrame in your change handler.
You get the idea. You need your check to happen at frame 99.

falling bottle not falling

I want to create a game of falling objects. In this case I have only one bottle and i want it to fall but it's not working. As you can see in the picture, the bottle starts to distort, it's not falling. Thanks!
function bottleCreate(e:Event):void {
var bottleNew:MovieClip;
bottleNew = newBottle();
bottleNew.x = 100;
bottleNew.y=0;
addChild(bottleNew);
bottle.addEventListener(Event.ENTER_FRAME, bottleMove);
}
function bottleMove(e:Event):void {
e.target.y ++;
}
stage.addEventListener(Event.ENTER_FRAME, bottleCreate);
You should remove the event listener for bottleCreate function, by
adding
stage.removeEventListener(Event.ENTER_FRAME, bottleCreate);
to your bottleCreate function.
Or
Call bottleCreate function once , instead of using
stage.addEventListener(Event.ENTER_FRAME, bottleCreate);
It doesn't get distorted. You are adding new bottle every frame with (100,0) so it looks like it is distorted but actually there is a new instance every 1 pixel
call bottleCreate() only once

Reverse/Playback AS3 Timeline

I'm trying to make a reversed play module in Action Script 3. I have a video, 200 frames long that I imported to Flash as a movie clip. I name the movie clip and inserted some key frames to make the video stop at specific frames, making it a 3 stage animation.
Whenever I swipe/pan to the right (detecting a positive x offset) it gives the command play();, the movie clip will play til it finds a stop.
What I want to achieve is to play it backwards from the current frame til the previous stop when I swipe to the left (detecting a negative offset).
I sorted out the swipe/touch programming and what I'm missing is the backwards bit. I've managed to make it work, going backwards 1 single frame, not the whole bunch that exist prior to hit the previous stop frame. My code for the swipe and play forward is this, with the single prev frame included, which gives me just one frame back instead of the whole set before the previous stop.
Multitouch.inputMode = MultitouchInputMode.GESTURE;
mymovieclip.stop();
mymovieclip.addEventListener(TransformGestureEvent.GESTURE_SWIPE , onSwipe);
function onSwipe (e:TransformGestureEvent):void{
if (e.offsetX == 1) {
//User swiped right
mymovieclip.play();
}
if (e.offsetX == -1) {
//User swiped left
mymovieclip.prevFrame();
}
}
You could try this:
import flash.events.Event;
import flash.display.MovieClip;
//note that this is not hoisted, it must appear before the call
MovieClip.prototype.playBackward = function():void {
if(this.currentFrame > 1) {
this.prevFrame();
this.addEventListener(Event.ENTER_FRAME, playBackwardHandler);
}
}
function playBackwardHandler(e:Event):void {
var mc:MovieClip = e.currentTarget as MovieClip;
if(mc.currentFrame > 1 && (!mc.currentFrameLabel || mc.currentFrameLabel.indexOf("stopFrame") == -1)) { //check whether the clip reached its beginning or the playhead is at a frame with a label that contains the string 'stopFrame'
mc.prevFrame();
}
else {
mc.removeEventListener(Event.ENTER_FRAME, playBackwardHandler);
}
}
var clip:MovieClip = backMc; //some clip on the stage
clip.gotoAndStop(100); //send it to frame 100
clip.playBackward(); //play it backwards
Now you can put 'stopFrame' label to the clip's timeline (stopFrame1, stopFrame2... stopFrameWhatever) and the clip should stop there until playBackward is called again. Note that you should remove the enter frame event listener if the clip hasn't reached a stopFrame or its beginning and you want to call play/stop from the MovieClip API, otherwise it may cause problems.