make a movieclip move on motion path - actionscript-3

i am making a snake and ladder game for my project.
My game is almost done except that now i need to make the game piece follow a "motion path" to go up the ladder "smoothly" instead of straight jumping to the tile that the ladder ends at.
I have been searching on how to make a movie clip move on motion path. But none of them are useful to me as i only need the game piece to move along the motion path when it reaches a special tile(eg. ladder or snake).
So my question is how do i exactly code my game such that when the game piece reach a special tile, it "snaps" onto the motion path and move accordingly to the path?
Here's my current code that i used to move my game piece up the ladder :
if(playerTile[i] == startLadder1 ){
ladder4.play();
playerTile[i] = destLadder1;
players2[i].x = tiles[playerTile[i]].x + offsets[i].x;
players2[i].y = tiles[playerTile[i]].y + offsets[i].y;
}
playerTile is the current tile that the game piece is on.
startLadder1 is the tile at the start of a ladder.
destLadder1 will be the end of a ladder.

Use a Tweening engine, such as Tweensy or TweenLite. For example, Tweensy's Bezier2D would probably give you the curve you wanted.

Related

How do I create a simple on-mouse-click rotating object in flash

This question may get downvoted or go unanswered because it's not the greatest and incredible silly but anyway,
I'm taking an intro level Webanimation course this semester at UNI and had I know what their expectations are for our hand-in projects i would have never taken it up.
Basically the teacher taught us stuff to the extent of masking/tweening and very few mouse-event and basic function codes.
Now she is expecting us to make a god damn ENTIRE PIPE GAME. The one where there are a bunch of rotating pipes and you gotta rotate them in place before a timer runs out and then the water flows through them.
For this project I have to somehow figure out the following (even though she didn't teach any of this):
-creature a grid of rotate-able pipes (one mouse click I assume would do a 90 degree classic tween rotation of the object)
-creature some sort of logic hit-box value chain to make pipes decide when to fill with water (they fill with water (a.k.a turn blue inside as an animation) once they are connected to another water filled pipe, for example)
-creature multiple levels and a menu screen
-add a music track.
Now i know this site is for specific help only and you basically can't ask for help on an entire project, so for now if somebody could just help me out with the following:
How do I create a rotating pipe on mouseclick?
So I have my pipe movieclip created and I have my Mouse Event code ready but I don't have the faintest on how to make a tween within the pipe and connect it to the code so that it rotates on mouseclick.
So this far, let's say for one of the pipes, instance pipe_1, I want to do this:
pipe_1.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler);
function fl_MouseClickHandler(event:MouseEvent):void
{
trace("Mouse clicked");
}
I also have the simple tween of rotation already created within the instance of the pipe, but dunno how to connect it to code.
I'm supposed to figure out what to put inside the function but I honestly have no clue. Hours of googling have come up with nothing either except a 12 dollar purchasable source code for an even more complicated pipe game.
I hope somebody can at least help a bit, and thanks.
The way to rotate a clip is via it's rotation property. It defaults to 0.
If you were set the rotation property of your tile to 90, you'd rotate your pipe tile 90 degrees.
for example :
pipe_1.rotation += 90;
A tween is a means of changing a property of a given DisplayObject over time. So what you want to do is tween your rotation property 90 degrees over time.
Here is a tutorial on Tweening - http://www.republicofcode.com/tutorials/flash/as3tweenclass/
I think it'd be more beneficial for you to take the time to learn about it, than to have me just write a few lines of code to solve your problem.
StackOverflow is a place where you can ask a question, AFTER you have tried something and have hit an issue.
I have provided you with the basic concept of what you need to do, and if you take the time to learn about tweening, you'll be able to achieve your goal rather simply.
There are also tweening libraries such as TweenLite and TweenMax that simplify tweening. Not sure if your class will allow you to use them, but worthwhile to check out for your own benefit.
You can find TweenLite here :
http://www.greensock.com/tweenlite/
Are you talking about a frame by frame tween? or tweening with code?
for frame by frame tweening, you can try to do this:
pipe_1.addEventListener(MouseEvent.CLICK, f1_MouseClickHandler);
function f1_MouseClickHandler(e:MouseEvent) {
pipe_1.gotoAndPlay(2); //if the tween starts at frame 2
}
For code tweening, just call the tween function inside that handler function

Move enemies in platform game

Im relative new to as3, this is my first question here.
Im making super mario game, I added tiles, collision detection, enemies, scrolling.
Sorry for my english, if you dont understand me well, I uploaded file here that you can understand better.
http://www.fastswf.com/K0pTu_0
When game starts all enemies are moving, what I want is that enemies move only if they are on the stage, or if they are near the player.
For example if I set enemy position enemy.x = 3000;
I dont want that he moves if player.x = 40 or something like that.
I tried something like this
if (enemy.x < stage.stageWidth)
{
enemy.x += enemy.speed;
}
But when enemy left the stage, he just stop and wont move anymore.
Btw, I crated one movie clip and I put everything inside it including player.
For example I scroll MovieClip to left, and character go right.
You may want to consider a pre-made platform. This has a free version that you can use to help develop something similar to what you are working on. The paid version has some nice features as well.
https://www.scirra.com/construct2

Flash - SideScroller Turret Math

I'm working on a side scroller, and for the enemy I'm making a turret. I'm trying to make the turret aim at the player but I cant seem to get it right. Below is a rough sketch of what I want to achieve:
I want the barrel (dark blue), to aim/rotate to its pointing at the player.
I have uploaded a YouTube video of my scene:
http://www.youtube.com/watch?v=eeP47VoX9uA&feature=youtu.be
This is what I have so far (loop):
function enterFrameHandler(e : Event) : void{
_turretBarrel.rotation = Math.atan2(enTarget.x, enTarget.y) * 180/Math.PI;
}
What this does is only rotate the barrel when I jump, and the barrel isn't even aiming at the player, also the barrel doesn't change rotation when I walk on the other side of the turret.
My enTarget.x is always central to the stage and the scene (including the turret) moves around the player left and right (x)... Only the enTarget.y moves (jump/high platform).
I'm slightly new to Flash and ActionScript. If anyone could help me out, or point me in the right direction then that would be great.
Thanks
1) Make sure you got the right numbers and the position of the avatar and the turret are in the same coordinate space. A simple trace of each would do. In this case you probably want the world (relative to stage) position of both clips. Make sure they make sense compared to top left corner of the screen (0, 0).
2) Remember that _turretBarrel.rotation is a rotation that ranges from -180 to 180 so this would need to be taken into consideration when calculating angles.
3) Make sure you use the corresponding degrees/radians where appropriate.
4) force focus on avatar, run the game and see if the bounds looks ok. Then do the same thing with the turret.
Another good thing in general for debugging purposes is to setup some kind of debug graphics. i.e. draw a line of what you think is the direction vector to verify your numbers and calculations.
On a side note:
This is what the majority of programming is; Debugging. Assume nothing but hard facts, get your numbers from the debugger (probably quicker), or trace output. If you're still using the horrible flash professional IDE. I would really recommend getting one with a proper debugger like FlashDevelop (free) or Flash Builder (commercial)
Oliver, it looks like you are calculating the tangens of wrong angle (between player and X-axis). You need something like the following:
function enterFrameHandler(e : Event) : void{
_turretBarrel.rotation = Math.atan2(enTarget.x - barrel.x, enTarget.y - barrel.y) * 180/Math.PI;
}

Moving the "camera" of an HTML Canvas element

I'm trying to find a clean way to "move the camera" of a canvas element.
This for my prototype game (side scroller). I'd love to think there's a better solution than moving the whole set of nodes to simulate a "camera" moving around.
Am almost certain to have read a simple how-to (using offsets?) but the fact I don't find anything like that starts to raise doubts... have I imagined reading that!?
Thanks to help me clarify...
J
Presumably you redraw your whole game scene 30 times a second (more or less)
You need to redraw your whole game scene but first translate the Canvas context by some offset.
context.translate(x,y) is precisely what you want. You'll want to read up on the use of that as well as the save() and restore() methods.
When you translate the context, everything drawn afterwards is shifted by that amount.
So you constantly draw something (maybe an enemy) at 50,50 using drawImage(badguy,50,50). Then the player moves, which changes the x of translate to -1 (because the player is moving to the right) instead of 0. You still draw the enemy sprite with the command drawImage(badguy,50,50), but when you draw it the enemy shows up as if it were at 49,50 because of the context.translate(-1,0) command shifting everything before its drawn.
Of course when you get into performance you'll want to be making sure that you are only ever drawing things that can actually be seen on the screen! If your are far down the level with context.translate(-2000,0), you dont want to be drawing objects at 50,50 anymore, only ones that intersect the viewable area.

How can I get the object Type from a HitTest?

For practice, I'm coding a really simple 2D platforming game in Flash using AS3. I'd like to define two different types of terrain surfaces that the player can walk on based on classic platforming elements. Type1: the player can walk on, and if the player jumps, they will hit their head on it and bounce back to the ground. Type2: the player can also walk on, but if the player jumps and hits their head, they will simply pass through the surface and not bounce back to the ground.
I am using hitTestPoint to resolve collisions for this. My question is: What would be the best method to test for what TYPE of ground I am colliding with? Each ground type has it's own Class associated with it in my Flash IDE and all the different terrain surface types are in the same movie clip on the stage.
Currently I'm testing to see if it hit one type of ground surface, then i'm testing if it hit the other, and then based on those results, I process what I want to happen. This seems to work okay right now, but I'm imagining that I may want to create more than 2 types of ground to collide with. For example, moving platforms. It seems like the code will start to get complex
Eg.
if(_groundType1.hitTestPoint(_player.x, _player.y, true))
{
if(_groundType2.hitTestPoint(_player.x, _player.y, true))
{
//don't hit the players head
}
//hit the players head
}
Try using instanceof - http://www.adobe.com/support/flash/action_scripts/actionscript_dictionary/actionscript_dictionary389.html