How to make a movieclip appear and reappear in the same location? - actionscript-3

Well, I'm working on a shooting game in which I have to click enemies to make two counters count the number of hits I get. In this case the enemies are ghosts and I tried making some kind of animation when they are shot so the animation replaces them after I click on them.
However I can't seem to make them reappear again in the same location, it's a very simple drawing made with the shape and drawing tools of Flash CS5.
The code I use in this case for the shooting part is this one:
function disparar (event:MouseEvent):void{
contar +=1;
disparos.text = contar.toString();
var destino1:Boolean = this.mira.hitTestObject(this.FRojo)
if (destino1 == true){
cuenta +=1;
aciertos.text = cuenta.toString();
this.FRojo.visible = false;
colorante.color = 0xFF0000;
this.bang.transform.colorTransform = colorante;
this.bang.x = this.FRojo.x;
this.bang.y = this.FRojo.y;
this.bang.scaleX = this.FRojo.scaleX;
this.bang.scaleY = this.FRojo.scaleY;
this.bang.play();
this.FRojo.visible = true;
}
In fact you can see the whole file in here, it's very simple, but I can't seem to make the movieclip either disappear and reappear or make the animation that follows reappear each time I hit one of the ghosts. Could anyone help me with this? I'd really appreciate the help.

You're hiding the movie clip by setting this.FRojo.visible = false; and then setting it back to true pretty much right after so it never hides.
Try replacing this.FRojo.visible = true;
with
setTimeout(function(){FRojo.visible = true;}, 1000);
That will make it reappear after 1 second (1000 milliseconds).
So remove this and make sure each one is FRojo, FAzul and FMorado.
If you use a tweening library like greensock you can do some cool fades rather than it just reappearing.
EDIT
You have another error inside your BANG movie clip. On frame 10 replace
this.bang.x = 50;
this.bang.y = 10;
this.bang.scaleX = 1;
this.bang.scaleY = 1;
with
this.x = 50;
this.y = 10;
this.scaleX = 1;
this.scaleY = 1;

Related

How to make a slider where you have to hit the middle of it in order to cause it to go to another scene

I am new to flash and want to make a slider where something moves back and for until you hit space bar in order to make land in the middle which would trigger a new scene. Sort of like a mini game.
Again im really new to actionscript so any help would be nice. I was thinking of calling like a function where the pointer is continuously moving until the space bar is hit. But Im really stuck. Please and thank you for your time.
Make sure your slider has the instance name "slider". Then try this code:
//x position of the most left position of the slider
var left_limit:Number = 50
//x position of the most right position of the slider
var right_limit:Number = 250
var speed:Number = 10
var dir:int = 1
//How close the slider has to be to the middle to trigger the next scene
var tolerance:Number = 10
addEventListener(Event.ENTER_FRAME,loop)
addEventListener(KeyboardEvent.KEY_DOWN,keydown)
function loop(e:Event){
slider.x += speed*dir
if(slider.x>right_limit){
slider.x = right_limit
dir = -1
}else if(slider.x<left_limit){
slider.x = left_limit
dir = 1
}
}
function keydown(e:KeyboardEvent){
if(e.keyCode==Keyboard.SPACE){
removeEventListener(Event.ENTER_FRAME,loop)
if(Math.abs((slider.x-left_limit)-(right_limit-left_limit)/2)<tolerance){
//succeeded go to next scene
gotoAndStop("scene2")
}else{
//failed
}
}
}

Snakes and Ladders AS3.. how can a movie clip climb a ladder?

I am having this problem in developing this snakes and ladders game and i am very much hoping that you guys can help me out. i already created the board and the avatar. only thing is i cant make the avatar move up the ladder, and move down with the snake. can somebody help me? i am very much desperate right now, and every help is appreciated, thank you guys!
EDIT:
here's the code that i have written so far here are some of the codes I have written so far..
stop();
var xCoord:Array = [141,251,360,471,580,691,799,910,1019,1127,1238,1238,1127,1019,910,799,691,580,471,360,251,251,360,471,580,691,799,910,1019,1127,1238,1238,1127,1019,910,799,691,580,471,360,251,251,360,471,580,691,799,910,1019,1127,1238,1238,1127,1019,910,799,691,580,471,360,251,251,360,471,580,691,799,910,1019,1127,1238,1238,1127,1019,910,799,691,580,471,360,251,251,360,471,580,691,799,910,1019,1127,1238,1238,1127,1019,910,799,691,580,471,360,251];
var yCoord:Array = [675,670,670,670,670,670,670,670,670,670,670,602,602,602,602,602,602,602,602,602,602,534,534,534,534,534,534,534,534,534,534,466,466,466,466,466,466,466,466,466,466,399,399,399,399,399,399,399,399,399,399,331,331,331,331,331,331,331,331,331,331,262,262,262,262,262,262,262,262,262,262,195,195,195,195,195,195,195,195,195,195,127,127,127,127,127,127,127,127,127,127,60,60,60,60,60,60,60,60,60,60];
var arrSquares:Array = new Array(xCoord.length);
var spaceIndex:Number = 0;
var delay:Number = 400;
var tm:Timer = new Timer(delay);
tm.addEventListener(TimerEvent.TIMER, mover);
tm.addEventListener(TimerEvent.TIMER_COMPLETE, moveDone);
spinner.addEventListener(MouseEvent.CLICK, doSpin);
var total:Number =0;
function doSpin(mevt:MouseEvent):void {
var rn:Number = Math.round(5*Math.random()+1);
txtCount.text = String(rn);
total = total + rn;
txtTotal.text = String(total);
txtCount.visible = true;
spinner.removeEventListener(MouseEvent.CLICK, doSpin);
tm.reset();
tm.repeatCount = rn;
tm.start();
}
function mover(tevt:TimerEvent):void {
spaceIndex = (spaceIndex+1)%(xCoord.length);
chip.x = xCoord[spaceIndex];
chip.y = yCoord[spaceIndex];
}
function moveDone(tevt:TimerEvent):void {
spinner.addEventListener(MouseEvent.CLICK, doSpin);
txtCount.visible = false;
}
i dont know where to put the if statement of executing the motion tween attached to the chip(avatar)
Well here are my initial thoughts. I would create an animation with your character who you want to move up and down. To make it simple, make an animation for climbing, then one for falling. It's really easy to do, just use the tween option on the timeline after inserting your keyframes.
Looks like you've created your animations. Now, whether or not he slides up or falls down depends on where your snakes and ladders are. Only you know that, so at those particular spots on your board (for example, x = 200 and y = 200) we want the if statement to occur. But we only want it to occur after your avatar is done moving. Probably create a new function and add the following to the mover function
var checkX:Number = chip.x;
var checkY:Number = chip.y
checkLandingSpace(checkX,checkY);
Now let's make a function checkLandingSpace which will check if we are on a shoot or ladder
function checkLandingSpace(checkX:Number,checkY:Number):void
{
if (checkX = (your point) || (another point) //first check all x points...continue like this for all points where a ladder is...the || means or
{
if (checkY = (your points....etc)
{
mc_avatarLadder.play(); //choose your instance name to be mc_avatar, then play the tween
}
}
}
Or check this post for equating one variable to any element in an array If [Get Variable] is equal to [Array]
Now write the that for your ladder. Then do the exact same thing with all of your shoots points. Except, make a new animation with instance name mc_avatarShoot then just say mc_avatarShoot.play();
After that, make sure on the last frame for both instances, you just put in gotoAndStop(1); just to make sure it's ready next time you go to play the animation.
Of course, adjust your timer appropriately. This should pretty much do it, there might be a couple things to adjust for your own game, but follow this and understand it and you'll get it.

AS3: Camera-Drag is jerking

I have a big problem because I need to move my camera by dragging the mouse (finger on mobile) to a position you want.
The code itself works fine but I have a bad jerking while dragging.
My Code:
if(_drag)
{
var mousePos:Point = new Point(_gameRef._stage.mouseX, _gameRef._stage.mouseY);
var localPos:Point = _gameRef.MainElement.globalToLocal(mousePos);
var diffX:Number = MathHelper.Difference(_prevMPosX, localPos.x);
var diffY:Number = MathHelper.Difference(_prevMPosY, localPos.y);
// Add the position changes for the camera
if(localPos.x < _prevMPosX)
_gameRef.CamController.x += diffX;
if(localPos.x > _prevMPosX)
_gameRef.CamController.x -= diffX;
if(localPos.y < _prevMPosY)
_gameRef.CamController.y += diffY;
if(localPos.y > _prevMPosY)
_gameRef.CamController.y -= diffY;
// change the previous mouse position to the current
_prevMPosX = localPos.x;
_prevMPosY = localPos.y;
e.updateAfterEvent();
}
The funny thing is that when I change the + to - and opposite then it works without jerking (but it doesn't feel natural so it's not the solution to my problem).
Thanks a lot!
Alright, the fault was to use the local coordinates.
No I've made every dependency from my global stage coordinates and it works without any jerking!

How to make smooth moving using as3?

I have loaded some images through XML and attached into dynamically created MovieClips named mc0,mc1,mc2...etc.
_loader.removeEventListener(ProgressEvent.PROGRESS, onLoadingAction);
count++;
var img:Bitmap = Bitmap(e.target.content);
img.cacheAsBitmap = true;
img.smoothing = true;
img.alpha = 0;
TweenLite.to(MovieClip(my_mc.getChildByName("mc"+count)).addChild(img),1, {alpha:1,ease:Quint.easeIn});
and within ENTER_FRAME handler
for (i=0; i < mc.numChildren; i++)
{
my_mc.getChildAt(i).x -= Math.round((mouseX-stage.stageWidth/2)*.006);
}
Everthing works fine. But it is shaking so that it was not looking good.
How do I achieve smooth movement?
One solution I've used is to round the (x,y) position to the closest integer. No matter that you've added smoothing to your bitmap and cached it, rounding could make it feel less choppy and way smoother.
Another thing you need to be careful is the dimensions of the images. Images that have an odd dimension won't be smoothed the same way as images with even dimensions. Check how to workaround this in my blog post Flash Smoothing Issue.
Since Flash has a variable frame rate (in the sense that it will drop frames), one shouldn't depend on the entering of a frame as a unit of action. Rather, it would be wiser to calculate the elapsed time explicitly.
For instance, in the enter frame handler:
var currentTime:Number = (new Date()).time;
for (i=0; i < mc.numChildren; i++)
{
my_mc.getChildAt(i).x -= speed * (currentTime - lastTime); // speed is in px/ms
}
lastTime = currentTime;
where you have the variable lastTime declared somewhere in a persistent scope:
var lastTime:Number = (new Date()).time;
I don't know if this addresses what you are calling "shaking", but it's at least something to consider.

Detect position?

I'm trying to create a simple game with a boat moving between left and right by the keys. The moving is OK, but when I try to detect the left och right end it doesn't work at all. Below is a part of the code. What could be wrong?
stage.addEventListener(Event.ENTER_FRAME,moveBoat);
function moveBoat(event:Event):void {
if(! boat.x >= 700){
if(moveLeft) {
boat.x -= 5;
boat.scaleX = 1;
}
if (moveRight) {
boat.x += 5;
boat.scaleX = -1;
}
}
}
If you've solved your collisions problem, here's an answer for your dropping bombs problem. Doing it by having 5 boolean variables would be a rather unrefined way of doing it; instead simply use an integer to record how many bombs your boat has left to drop, and each time it drops one, reduce this value by 1. Here's some example code:
//Create a variable to hold the number of bombs left.
var bombsLeft:int = 5;
//Create an event listener to listen for mouse clicks; upon a click, we'll drop a bomb.
addEventListener(MouseEvent.CLICK, dropBomb);
//The function dropBomb:
function dropBomb(event:MouseEvent):void
{
if (bombsLeft > 0)
{
//Create a new instance of the Bomb class; this could be an object in your Library (if you're using the Flash IDE), which has a graphic inside it of a bomb.
var newBomb:Bomb = new Bomb();
//Position the bomb.
newBomb.x = boat.x;
newBomb.y = boat.y;
//Add it to the stage
addChild(newBomb);
//Reduce the number of bombs you have left.
bombsLeft--;
}
//At this point you could check if bombsLeft is equal to zero, and maybe increase it again to some other value.
}
This doesn't include code to then move the bomb downwards, but you can do that fairly simply using an update loop. If you're struggling to do that, let me know and I'll give you another example.
Hope that helps.
debu