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

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);

Related

AS3 removeChild and collisions : issue

I'm working in a little shooting game with Flash and AS3. I'm still beginning for using Class documents, but I managed to understand thanks to tutorials.
So here's how it works :
I used the HitTestObject when the Enemy MovieClip hits a bullet, shot by the user :
//checking if it is touching any bullets
//we will have to run a for loop because there will be multiple bullets
for(var i:int = 0;i<_root.bulletContainer.numChildren;i++){
//numChildren is just the amount of movieclips within
//the bulletContainer.
//we define a variable that will be the bullet that we are currently
//hit testing.
var bulletTarget:MovieClip = _root.bulletContainer.getChildAt(i);
//now we hit test
if(hitTestObject(bulletTarget)){
hit++;
if(hit==4)
{
gotoAndPlay(4); //the Enemy MC is removed after 4 hits(parent.removeChild(this))
removeEventListener(Event.ENTER_FRAME, eFrame);
//the Bullet MC is removed with its Listeners
_root.bulletContainer.removeChild(bulletTarget);
bulletTarget.removeListeners();
}
if(hit<4)
{
gotoAndPlay(2);
_root.bulletContainer.removeChild(bulletTarget);
bulletTarget.removeListeners();
}
if(hit > 4)
{
_root.bulletContainer.removeChild(bulletTarget);
bulletTarget.removeListeners();
hit = 0;
}
}
}
So after 4 hits, it is completely removed from the screen.
The eFrame function remove the Enemy MC if it goes off the stage :
private function eFrame (event:Event):void{
x+=speed;
if(this.x > stage.stageWidth + 50){
removeEventListener(Event.ENTER_FRAME, eFrame);
_root.removeChild(this);
}
}
I also put a command that detects collisions between the Enemy MC and the Player (that I gave the "craft" instance name) :
if(hitTestPoint(_root.craft.x -203.25, _root.craft.y - 44.9, collide))
{
_root.dmg.width+=8;//reduce the life bar
removeEventListener(Event.ENTER_FRAME, eFrame);
gotoAndPlay(4);
}
if(hitTestPoint(_root.craft.x -210.6, _root.craft.y - 32.9, collide))
{
_root.dmg.width+=8;
removeEventListener(Event.ENTER_FRAME, eFrame);
gotoAndPlay(4);
}
if(hitTestPoint(_root.craft.x -210.6, _root.craft.y - 6, collide))
{
_root.dmg.width+=8;
removeEventListener(Event.ENTER_FRAME, eFrame);
gotoAndPlay(4);
}
if(hitTestPoint(_root.craft.x -211.4, _root.craft.y + 20, collide))
{
_root.dmg.width+=8;
removeEventListener(Event.ENTER_FRAME, eFrame);
gotoAndPlay(4);
}
...//There is a lot of other points with the same results...
So the Enemy MC is also removed if it hits the craft MC (Player). But I encountered an issue : when the Enemy MC is hit (4 times by the Bullet movieclips "shot" by the user which removes it) but also collided by the player, it is not removed as it ought to be...
It is instead still displayed on the screen, and is static : it can't be hit neither by a bullet MC, neither by the player MC...
I think this is because of the parent.removeChild(this) executed from gotoAndPlay(4) of the Enemy MC, which causes that issue : it is executed a twice (because the Enemy MC is destroyed by the Bullet MC AND by the Player)...
Anyone has an idea to solve that double execution of removeChild ?
If you want to have more info, you can download the source :
https://www.dropbox.com/s/wvl116wjqpu69d0/shoot_aircraft%20game.zip?dl=0
Thank you for your help.
Put your enemy inside of a container (could be just a Sprite object) and on the 4th frame of the animation call
parent.parent.removeChild(this.parent);
You'll have to change a few of your enemy movement lines of code maybe but you can figure that out.
Basically the idea is to hide the animation inside of a non animating Sprite. Changing frames mid code screws things up, unless it's nested deeper than the object that is being called on. I hope that makes sense.

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

AS3 movieclip trigger another movieclip

I just want to clarify, can a movieclip that triggers by interaction trigger another movieclip?
I am doing a function which using blowing function which is working by the way.
function doEveryFrame(e:Event)
{
trace(mic.activityLevel);
if(mic.activityLevel == 100)
{
ballMC.gotoAndPlay(1);
}
}
After above interaction ballMC reaches my rectangle graphic. From the rectangle, I want my blueballMC to just play.
I hope this is clear enough, though.
Yes, this is more than possible by:
1: First check if the movieclip is on the frame you want it to be on (if you want it to be on the last frame, you can check the movieclip's totalFrames keyword): if(yourMovieClip.currentFrame == frameYouWantItToBeOnToTrigger)
2: Trigger the next movieclip: if(yourMovieClip.currentFrame == frameYouWantItToBeOnToTrigger)
{
nextMovieClip.gotoAndPlay(frameNumberForNextMovieClipToStartOn);
}
And that's all there is to it.

Changing individual movieclip's frame rate with TweenMax

My aim is to change the frame rates of my individual (looping) movie clips through clickable controls (slow/med/fast). I've heard it isnt possible to achieve this through as3/flash alone, so I've tried greensock's TweenMax... However I can't seem to figure out how to do this. Is there anyone that could help?
box1.addEventListener(MouseEvent.MOUSE_DOWN, box1down);
function box1down(event:MouseEvent):void {
//FRAMERATE CODE HERE
}
Many thanks!
Here is the API doc for TweenMax: http://www.greensock.com/as/docs/tween/com/greensock/TweenMax.html
If you have multiple movieclips that you are trying to control, you can just create an abstract class with the functionality you want and extend that class. So something like:
public class ControlledMovieClip extends MovieClip {
public function ControlledMovieClip() {
stop();
}
public function animate(frameRateInSeconds:Number):void {
TweenMax.to(this, frameRateInSeconds, { frame: this.totalFrames - 1, repeat: -1, ease: Linear.easeNone });
}
}
Have all your movieclips that are looping extend that class, and then you could call the animate function on the objects in your box1down event handler.
I haven't tested that code so you might need a gotoAndStop(1) at the end of each iteration.
It's possible through Actionscript alone it just requires you to handle the frame progression yourself (instead of using mc.play() you stop the movieclip and call nextFrame() yourself).
Lets say a Movieclip (myMC) has 20 frames of animation. To manually run the animation at normal speed you simply call myMC.nextFrame(); on every frame of your project (using an ENTER_FRAME listener for example).
To have the animation run at half speed you can use a frame count and a frame trigger:
var frameTick = 0;
var frameAnimTrigger = 2;
function Update(e:Event):void
{
frameTick++;
if(frameTick == frameAnimTrigger)
{
myMC.nextFrame();
frameTick = 0;
}
}
Because nextFrame is only called every other frame the animation appears to run at half speed.

AS3 When symbol is in a certain position

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);
}
}