Move enemies in platform game - actionscript-3

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

Related

AS3 collision detection in an object itself

I´m programing a space ship side scroll in as3. The bottom of the stage are mountains and here comes the problem, when I try to detect the ship collision against the mountains..
Because the poor collision detection and the need of avoid large loops my idea is create an object that works as a collider itself detecting a collision and avoiding parse all the stage or more selective metod.
I place "by hand" in the flash stage several instances of circles with a class for manage them where I place the If(this.collider.hits(ship)....
I spent looong time but I can find the way to make it work some of the mistakes i get are like this
Error 1061: Call to a possibly undefined method hitTestObject through a reference with static type Class.
some Idea? Thanks in advance
when you hit test with points it is important that the point being tested is relative to the object being tested against, eg
if(mountain.hitTestPoint(this.x + circle1.x, this.y + circle1.y))
will return true if the circles are inside the object calling the function because their position relative to the mountain is now relative to it rather then relative to the ships xy position within the clip... hope that makes sense.
btw I have done this myself in the past but I would have to remind you that you can only hit test with the points so there is no need to have circles, use blank sprites instead and set the visible flag in the properties panel to false, no drawing will make it slightly faster... not that you will notice, also sprites/graphics use less memory then movie clips.
also I would recommend hard coding some points in the clips rather then actually adding the clips in the sprite/clip itself, this will make it easier to work with them and scale later on (believe me this will annoy the hair from your head to do something later and slow the game to scale on the fly)
try something like this... you can determine the points values by adding a clip to the movie clip and getting its position from the properties if you must.
private var hitPoints:Vector.<Point> = new Vector.<Point>
hitPoints.push(new Point(10, 40));
hitPoints.push(new Point(30, 40));
//...do this for all your points
//loop through all your points and check if the hit relative to the ships position.
for(var i:int = 0; i < hitPoints.length; i++)
{
if (scene.hitTestPoint(ship.x + hitPoints[i].x, ship.y + hitPoints[i].y))
{
//do your hit stuff here
break;//don't forget to break
}
}
in this code you will need to make sure the scene object is a reference to your scenery at the bottom of the screen.
I hope this helps but if this is not enough help then you should post some of your code here so we can have a look and see where it can be improved.

Position fixing to moving movie clip not acting instantly

Hi there,
I'm making a game prototype, a “hero” which is controlled by the mouse when clicked. To visualize the player's velocity direction and the direction from hero to mouse, I have made two MovieClip classes: “Direction_mark” and “Direction_pointer”, respectively. They are NOT nested due to issues I've run into when dealing with the possibility.
When trying to make “pushing camera” (where the hero moves and the background is fixed until you reach a certain point near the stage limits and the environment starts to move), though, I had to make the “Directions” movie clips stay in the same position as the hero, even if this moves. So I simply wrote this (the event listener for the function is onEnterFrame):
//In the Direction_mark and Direction_pointer class:
//...
function update (e: Event){
//code...
this.x = Game.hero.x;
this.y = Game.hero.y;
//more code...
}
//...
But it doesn't work: I experience a certain “easing”, the “Directions” follow the hero in and elastic-like effect which is not desired at all.
Does somebody know why this happen, and how to solve it? This is the first time I experience this problem. Any help, advice or comment would be greatly appreciated.
Thank you very much in advance.
PD: Probably not relevant, but I'm using CDK to manage collisions. Maybe takes too much CPU and makes it work wrong?

Movie Clips frozen but swf has not crashed - AS3 CS6

I'm making a top down shooter for a college project. I'm getting extremely low on available time left to complete this.
I followed a tutorial as we was not taught any AS3 although expected to creating a game in this language so there is commented code due to this. The tutorial lead me up to creating a player on the stage that moves by keyboard presses and can fire bullets by pressing the left mouse button, but it didn't help with creating enemies at all.
So I created an enemy movie clip that just spawns on the stage at X=0 and Y=0 (random spawning will be added later), and it's supposed to move towards the players location but it doesn't. When the swf loads up it appears they have started to move but have frozen on the screen. It's not crashed because I can still move the player around and fire bullets? Any help is appreciated.
Main.as code
Enemy.as code
Well, you are not calling the moveTowards() in any loop -> the position is not updated. Or where exactly are you updating the enemy's position? I see only one setting of .x, .y of enemy and it is right after you create the object, no further updates.
Put this into your loop function in Main.as:
for(var i:int=0; i<enemies.length; i++) {
Enemy(enemies[i]).moveTowards(player.x, player.y);
}

make a movieclip move on motion path

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.

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