Snakes and Ladders AS3.. how can a movie clip climb a ladder? - actionscript-3
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.
Related
How to display player lives in game AS3
Hey everyone so having a little trouble trying to accomplish this. I understand how to add lives and display them through a Dynamic Text Field on the stage. My game is set up right now to where this happens and whenever the player dies a live decrements so it works fine. But I want to display and Image of all 3 lives a custom Movie Clip that I drew in Flash CS6 to represent the lives. So All 3 lives will be displayed in the game and once the player dies one of the lives images is removed. I have some idea on what to do. For instance I created a "for loop" to display all 3 images on the screen and created variables to place them horizontally next to each other with a space of 30 pixels. But I'm not sure if this is the correct way to approach this. Also kind of confused on how I would remove one of the images when the player dies? Her is my code so far: public var playerLives:mcPlayerLives; private var nLives:Number = 3; private var startPoint:Point; private var aPlayerLivesArray:Array; In my main class Engine: aPlayerLivesArray = new Array; addPlayerLivesToStage(); Then the addplayerlivestostage function: public function addPlayerLivesToStage():void { var startPoint:Point = new Point((stage.stageWidth / 2) - 300, (stage.stageHeight / 2) - 200); var xSpacing:Number = 30; for (var i = 0; i < nLives; i++) { trace(aPlayerLivesArray.length); playerLives = new mcPlayerLives(); stage.addChild(playerLives); playerLives.x = startPoint.x + (xSpacing * i); playerLives.y = startPoint.y; aPlayerLivesArray.push(playerLives); } } So like i stated above everything works fine and it does display the 3 images that represent the lives, but would this be the correct approach or is there an easier method?
i think you're using right way to add lives. and for removing them, you don't need to remove all and add new lives, you can remove the last element of lives array, so, it's done, i think thats already an easy method so, you can implement this for (var i = 0; i < nLives; i++) to make a better usage for "adding lives in-game (earning lives)" something like for (var i = aPlayerLivesArray.length; i < nLives; i++) but don't forget to decrease array length by 1 after removing last element of livesArray when player dies
Looks pretty close to a reasonable approach. I would have a blank container movieclip on stage where you want the lives icons to display. Create one life icon in your library and link it for export with actionscript. When you generate your game view, you can populate this container with the starting value of three lives. Place the first one, then place the subsequent ones based on the first location. Some untested code: NOTE: The following presumes your container clip for the lives is named lives_container and your link instance name for the life icon in the library is Life_icon. var numberOfLives:Number = 3; var iconSpacing:Number = 5; var nextX:Number = 0; for(var i:int = 0; i < numberOfLives; i++ ) { var icon:MovieClip = new Life_icon(); icon.x = nextX; lives_container.addChild( icon ); nextX += icon.width + iconSpacing; } This way you could add extra lives easily if the player gained any by adding new icons at the last nextX value, like so: function addLife():void { var icon:MovieClip = new Life_icon(); icon.x = nextX; lives_container.addChild( icon ); nextX += icon.width + iconSpacing; } or remove them as the player loses them: function removeLife():void { var numberOfLivesDisplayed:Number = lives_container.numChildren(); lives_container.removeChildAt( numberOfLivesDisplayed - 1 ); nextX -= icon.width + iconSpacing; } Using a container clip for the lives icons makes adjusting the location of the life icons easier if it becomes necessary later.
AS3 Psuedo 3D Z-Indexing
I am attempting to make a really basic Pseudo 3D game in AS3. When I press certain keys my character moves up and down but what I want to happen is when the characters y position is above an objects y position then the character should appear behind the object. Here is my code for an objects class at the moment: package { import flash.display.MovieClip; import flash.utils.getTimer import flash.events.Event; public class bushMC extends MovieClip { private var lastFrame:int = new int(0); private var dt:Number = new Number(); private var main:Main; public function bushMC(){ main = this.parent as Main; stage.addEventListener(Event.ENTER_FRAME, update); trace(main.getChildIndex(this)); } private function update(e:Event):void{ dt = (getTimer() - lastFrame)/30; lastFrame = getTimer(); if(main.char.y + 200 < this.y + 55 && main.getChildIndex(main.char) > main.getChildIndex(this)){ main.setChildIndex(this, main.getChildIndex(main.char)+1); } else if(main.getChildIndex(main.char) < main.getChildIndex(this)){ main.setChildIndex(this, main.getChildIndex(main.char)); } } } } I have tried editing loads of the values(+1, -1, equal to) for each calculation but I can't seem to find the right ones. One I tried almost works but instead when the char is supposed to be behind the object it simply flickers in-front and then behind continuously. Thanks in advance, Kyle.
I just tried a little quick mock script based off your code. I got it working how I assume you are attempting to get it to work: import flash.events.Event; import flash.display.MovieClip; var char:MovieClip = new MovieClip(); var bush:MovieClip = new MovieClip(); char.graphics.beginFill(0xFF0000); char.graphics.drawCircle(0, 0, 30); bush.graphics.beginFill(0x00FF00); bush.graphics.drawEllipse(0, 0, 40, 80); this.addChild(char); this.addChild(bush); bush.x = 100+(Math.random()*350); bush.y = 100+(Math.random()*200); this.addEventListener(Event.ENTER_FRAME, updateYPos); function updateYPos(e:Event):void { char.x = mouseX; char.y = mouseY; if(char.y < bush.y + 30 && this.getChildIndex(char) >= this.getChildIndex(bush)){ this.setChildIndex(bush, this.getChildIndex(char)); } else if(char.y > bush.y + 30 && this.getChildIndex(char) < this.getChildIndex(bush)){ this.setChildIndex(bush, this.getChildIndex(char)); } } I hope this sample is enough to help you. All it needed was an extra condition on the else if and it works. :)
You should have a sorted list of bushes somewhere, which is then added via addChild() in the right order - uppermost bush has lowermost Z-position (child index 0 or the least of bushes, there could be other objects). Then, as your player moves, you track its position relative to list of bushes, so you don't run the full list check for z-ordering of player, but only check "nearest" bushes for possible change, then you set child index of player to found value. Note that if you're setting child index of player to bush's index, if you are moving player forwards (greater indexes), set to -1, as the bush will actually be one position lower because of player occupying a position in the display list, and if you are setting child index to lower values, set to equal. There is a more elegant version of this, using the fact that your bushes are continuous within display list, with only interruption being player, although it will run out of steam once more moving objects will appear. And yes, you run update on the player or any other moving objects, not on the bush. function updateZPos(e:Event):void { // process coordinates change var p:DisplayObjectContainer=this.parent; // a bit faster to use function local var to get parent var ci:int=p.getChildIndex(this); // changeable, get current index var prev:DisplayObject=null; if(ci>0) prev=p.getChildAt(ci-1); var next:DisplayObject=null; if(ci<p.numChildren-1) next=p.getChildAt(ci+1); while(prev) { if (this.y<prev.y) { ci--; p.setChildIndex(this,ci); if (ci>0) prev=p.getChildAt(ci-1); else prev=null; } else break; while(next) { if (this.y>next.y) { ci++; p.setChildIndex(this,ci); if(ci<p.numChildren-1) next=p.getChildAt(ci+1); else next=null; } else break; } } This function was written with implication of display list of p being pre-sorted, and will maintain sorted state of it after moving of this, and is suitable for any moving object, not just the player. For this to work without messing up your other objects, separate everything moving into one container which will then get referenced as base for sorting display list. Otherwise your player might eventually get above all as the last element to check will be say score textfield with Y of 0. Also you will need to maintain coherent position of register point all over your set of moving objects' classes, so that say the base of a bush will be at Y=0 instead of being at Y=30, as implied in your code. The legs of a player should then also be at Y=0.
How to make a movieclip appear and reappear in the same location?
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;
maintain ball speed box2d
I'm using this code to Create and throw the ball. I want to increase the speed of the ball and it should maintain the speed until it destroy. What should i do to getting the result?? var sphereShape:b2CircleShape=new b2CircleShape(15/worldScale); var sphereFixture:b2FixtureDef = new b2FixtureDef(); sphereFixture.density=1; sphereFixture.friction=3; sphereFixture.restitution=0.1; sphereFixture.filter.groupIndex = -1; sphereFixture.shape=sphereShape; ball_mc=new ballMc(); ballCont_mc.addChild(ball_mc); var sphereBodyDef:b2BodyDef = new b2BodyDef(); sphereBodyDef.type=b2Body.b2_dynamicBody; ball_mc.name="throwBall"; ball_mc.hitCount=0; ball_mc.basketHit=0; sphereBodyDef.position.Set(arrow_mc.x/worldScale,arrow_mc.y/worldScale); birdSphere=world.CreateBody(sphereBodyDef); birdSphere.CreateFixture(sphereFixture);girl_mc.gotoAndStop(3); birdSphere.SetUserData(ball_mc); birdSphere.SetBullet(true); var distanceX:Number=arrow_mc.x-mouseX; var distanceY:Number=arrow_mc.y-mouseY; var distance:Number=Math.sqrt(distanceX*distanceX+distanceY*distanceY); var birdAngle:Number=Math.atan2(distanceY,distanceX); birdSphere.SetLinearVelocity(new b2Vec2(distance*Math.cos(birdAngle)/4,distance*Math.sin(birdAngle)/4)); Can anyone help me?
Give the ball a Speed variable and use a function similar to the following: function run(Self) { Self._x+=Math.sin(Self._rotation*(Math.PI/180))*Self.Speed/10; Self._y+=Math.cos(Self._rotation*(Math.PI/180))*Self.Speed/10*-1; }//------------------------------------------------------------^run Call run(ball_mc); every frame you want it to move. Maintain the Speed variable as you would a velocity. Self is simply the ball. You will first need to set its rotation according to what direction it should be moving in, but that's a different topic so I'll save the space and leave that part out. I hope this helps.
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