Actionscript 3.0. How to calibrate collision detection? - actionscript-3

This is for a flash game that I am working on. I am trying to improve the collision detection between LevelOnePlayer and Wall11. However, even if I change the Wall Hit Detection variables by 0.1 pixels, the game keeps on detecting collision when the player is too close or too far from the walls. Is there anyway to make the collision detection pixel perfect?
const LEFT_WALL_HIT_DETECTION: Number=5.5;
const RIGHT_WALL_HIT_DETECTION: Number=5.5;
const UP_WALL_HIT_DETECTION: Number=5.5;
const DOWN_WALL_HIT_DETECTION: Number=5.5;
const ENEMY_HIT_DETECTION: Number=5.5;
var left:Boolean = false;
var right:Boolean = false;
var up:Boolean = false;
var down:Boolean = false;
var leftBumping:Boolean = false;
var rightBumping:Boolean = false;
var upBumping:Boolean = false;
var downBumping:Boolean = false;
var Onespeed: Number=3.5;
var LevelOneStartFrame: Number=1010;
addEventListener(Event.ENTER_FRAME, LevelOne);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
function keyPressed(event:KeyboardEvent): void{
if(event.keyCode == Keyboard.LEFT || event.keyCode == 65){
left=true;
}
if(event.keyCode == Keyboard.RIGHT || event.keyCode == 68){
right=true;
}
if(event.keyCode == Keyboard.UP || event.keyCode == 87){
up=true;
}
if(event.keyCode == Keyboard.DOWN || event.keyCode == 83){
down=true;
}
}
function keyReleased(event:KeyboardEvent): void{
if(event.keyCode == Keyboard.LEFT || event.keyCode == 65){
left=false;
}
if(event.keyCode == Keyboard.RIGHT || event.keyCode == 68){
right=false;
}
if(event.keyCode == Keyboard.UP || event.keyCode == 87){
up=false;
}
if(event.keyCode == Keyboard.DOWN || event.keyCode == 83){
down=false;
}
}
function LevelOne(event:Event){
if(left){
LevelOnePlayer.x-=Onespeed;
}
if(right){
LevelOnePlayer.x+=Onespeed;
}
if(up){
LevelOnePlayer.y-=Onespeed;
}
if(down){
LevelOnePlayer.y+=Onespeed;
}
if(Wall11.hitTestPoint(LevelOnePlayer.x-LEFT_WALL_HIT_DETECTION, LevelOnePlayer.y, true))
leftBumping=true; else leftBumping=false;
if(Wall11.hitTestPoint(LevelOnePlayer.x+RIGHT_WALL_HIT_DETECTION, LevelOnePlayer.y, true))
rightBumping=true; else rightBumping=false;
if(Wall11.hitTestPoint(LevelOnePlayer.x, LevelOnePlayer.y+DOWN_WALL_HIT_DETECTION, true))
downBumping=true; else downBumping=false;
if(Wall11.hitTestPoint(LevelOnePlayer.x, LevelOnePlayer.y-UP_WALL_HIT_DETECTION, true))
upBumping=true; else upBumping=false;
if(leftBumping) LevelOnePlayer.x+=Onespeed;
if(rightBumping) LevelOnePlayer.x-=Onespeed;
if(downBumping) LevelOnePlayer.y-=Onespeed;
if(upBumping) LevelOnePlayer.y+=Onespeed;
}

If you are looking for pixel perfect collision detection, you will need to change your methodology slightly and use BitmapData.hitTest(); instead of MovieClip.hitTestPoint(); You can read about the differences between the 2 functions / implementations here if you like.
To implement this, you can head over to the adobe docs, but here is the example snippet they provide (confusingggggg);
import flash.display.BitmapData;
import flash.geom.Rectangle;
import flash.geom.Point;
var bmd1:BitmapData = new BitmapData(80, 80, true, 0x00000000);
var rect:Rectangle = new Rectangle(20, 20, 40, 40);
bmd1.fillRect(rect, 0xFF0000FF);
var pt1:Point = new Point(1, 1);
trace(bmd1.hitTest(pt1, 0xFF, pt1)); // false
var pt2:Point = new Point(40, 40);
trace(bmd1.hitTest(pt1, 0xFF, pt2)); // true
In your case, you can do the following, but please note that I wrote the following without an IDE, so there may be a syntax error or two:
var Wall1Rect:Rectangle = Wall1.getBounds(this);
var Wall1BmpData = new BitmapData(Wall1Rect.width, Wall1Rect.height, true, 0);
var playerRect:Rectangle = LevelOnePlayer.getBounds(this);
var playerBmpData = new BitmapData(playerRect.width, playerRect.height, true, 0);
if(playerBmpData.hitTest(new Point(LevelOnePlayer.x, LevelOnePlayer.y), 255, Wall1BmpData, new Point(Wall1.x, Wall1.y),255))
{
trace("hit");
}
Another good tutorial.

Related

AS3 Restrict ship in visible stage area

Hey so i've been making a space shooter game and i created the movement but i'm trying to restricting the ship to the visible stage area and i can't figure out how to do that
also i'm sorry if my code it's really messy i'm really new at this
the stage size it's 1920 x 1080
here's the code of the movement
var leftKeyPressed: Boolean = false;
var rightKeyPressed: Boolean = false;
var upKeyPressed: Boolean = false;
var downKeyPressed: Boolean = false;
var xMoveSpeed: Number = 15;
var yMoveSpeed: Number = 15;
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
function keyDownHandler(e: KeyboardEvent): void
{
if (e.keyCode == Keyboard.LEFT)
{
leftKeyPressed = true;
}
if (e.keyCode == Keyboard.RIGHT)
{
rightKeyPressed = true;
}
if (e.keyCode == Keyboard.UP)
{
upKeyPressed = true;
}
if (e.keyCode == Keyboard.DOWN)
{
downKeyPressed = true;
}
}
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
function keyUpHandler(e: KeyboardEvent): void
{
if (e.keyCode == Keyboard.LEFT)
{
leftKeyPressed = false;
}
if (e.keyCode == Keyboard.RIGHT)
{
rightKeyPressed = false;
}
if (e.keyCode == Keyboard.UP)
{
upKeyPressed = false;
}
if (e.keyCode == Keyboard.DOWN)
{
downKeyPressed = false;
}
}
stage.addEventListener(Event.ENTER_FRAME, moveHero);
function moveHero(e: Event): void
{
if (leftKeyPressed)
{
ship.x -= xMoveSpeed;
}
if (rightKeyPressed)
{
ship.x += xMoveSpeed;
}
if (upKeyPressed)
{
ship.y -= yMoveSpeed;
}
if (downKeyPressed)
{
ship.y += yMoveSpeed;
}
}
Well, you can start with this.
// One map is much better than a separate variable for each and every key.
var keyMap:Object = new Object;
// Initial keymap values.
keyMap[Keyboard.UP] = 0;
keyMap[Keyboard.DOWN] = 0;
keyMap[Keyboard.LEFT] = 0;
keyMap[Keyboard.RIGHT] = 0;
// With the keymap you don't need these big event handlers,
// you need a single one with one line of code.
stage.addEventListener(KeyboardEvent.KEY_DOWN, onUpDown);
stage.addEventListener(KeyboardEvent.KEY_UP, onUpDown);
function onUpDown(e:KeyboardEvent):void
{
// The members of the object with the names of key codes
// will be set to 1 in case of KEY_DOWN event or 0 in case of KEY_UP.
keyMap[e.keyCode] = int(e.type == KeyboardEvent.KEY_DOWN);
}
stage.addEventListener(Event.ENTER_FRAME, moveHero);
// This will provide you with min and max values for both X and Y coordinates.
// You will probably need to adjust it because the ship have width and height.
var conStraints:Rectangle = new Rectangle(0, 0, 1920, 1080);
var xMoveSpeed:Number = 15;
var yMoveSpeed:Number = 15;
function moveHero(e:Event):void
{
var anX:Number = ship.x;
var anY:Number = ship.y;
// Use keymap to figure the new coordinates.
// Take into account all the relevant keys pressed.
anX += xMoveSpeed * (keyMap[Keyboard.RIGHT] - keyMap[Keyboard.LEFT]);
anY += yMoveSpeed * (keyMap[Keyboard.DOWN] - keyMap[Keyboard.UP]);
// Now set the new ship coordinates with the regard to constraints.
ship.x = Math.min(conStraints.right, Math.max(conStraints.left, anX));
ship.y = Math.min(conStraints.bottom, Math.max(conStraints.top, anY));
}
I think this is the easiet solution, tell me if it worked or not
function keyDownHandler(e: KeyboardEvent): void
{
if (e.keyCode == Keyboard.LEFT && ship.x > 15)
{
leftKeyPressed = true;
}
if (e.keyCode == Keyboard.RIGHT && ship.x < 1905)
{
rightKeyPressed = true;
}
if (e.keyCode == Keyboard.UP && ship.y > 15)
{
upKeyPressed = true;
}
if (e.keyCode == Keyboard.DOWN && ship.y < 1065)
{
downKeyPressed = true;
}
}

Flash Game Actionscript-3 Output Error #1009

I am making my own game using Actionscript 3 for coding in Flash. The game is about a Runner Character just like Super Mario and Stickyman, who just run, jump, dies.. and so on..
After being very organized by dividing each window of the game in a different Scene, I was still having the problem of executing the piece of code "gotoAndStop()".. That's why I decided to make it much simple by using only one Scene, but each wind of the game in a different frame in the Main Timeline. So I worked on the root, but still getting the same problem that stuck in a white screen!
Anyways, one of the main problem that I am facing here is the Error #1009. Even when I tried to read other topics with the same issue but I didn't find the answer that suites my case.
This is the Error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at HR4_fla::MainTimeline/movePlayer()
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Apple/update()
Here is the Code:
import flash.events.KeyboardEvent;
import flash.events.Event;
import flash.display.MovieClip;
import flash.geom.Rectangle;
stop();
var KeyThatIsPressed:uint;
var rightKeyIsDown:Boolean = false;
var leftKeyIsDown:Boolean = false;
var upKeyIsDown:Boolean = false;
var downKeyIsDown:Boolean = false;
var score:int = 0;
var lives:int = 3;
player_mc.health = 100;
player_mc.dead = false;
//var TouchRestartBox:Boolean = false;
var playerSpeed:Number = 8;
var gravity:Number = 2;
var yVelocity:Number = 0;
var canJump:Boolean = false;
var canDoubleJump: Boolean = false;
//var appleCount:int;
stage.addEventListener(KeyboardEvent.KEY_DOWN, PressAKey);
stage.addEventListener(KeyboardEvent.KEY_UP, ReleaseAKey);
stage.addEventListener(Event.ENTER_FRAME, cameraFollowCharacter);
function cameraFollowCharacter(event:Event):void
{
scrollRect = new Rectangle(player_mc.x - stage.stageWidth/2, player_mc.y - stage.stageHeight/2, stage.stageWidth, stage.stageHeight);
}
//PressKey function here
function PressAKey(event:KeyboardEvent):void
{
if(event.keyCode == Keyboard.RIGHT)
{
rightKeyIsDown = true;
}
if(event.keyCode == Keyboard.LEFT)
{
leftKeyIsDown = true;
}
if(event.keyCode == Keyboard.UP)
{
upKeyIsDown = true;
}
if(event.keyCode == Keyboard.DOWN)
{
downKeyIsDown = true;
}
}
//ReleaseKey function here
function ReleaseAKey(event:KeyboardEvent):void
{
if(event.keyCode == Keyboard.RIGHT)
{
rightKeyIsDown = false;
}
if(event.keyCode == Keyboard.LEFT)
{
leftKeyIsDown = false;
}
if(event.keyCode == Keyboard.UP)
{
upKeyIsDown = false;
}
if(event.keyCode == Keyboard.DOWN)
{
downKeyIsDown = false;
}
}
//stage.addEventListener(Event.ENTER_FRAME, GameOver);
stage.addEventListener(Event.ENTER_FRAME, movePlayer);
function movePlayer(event:Event):void
{
if(!rightKeyIsDown && !leftKeyIsDown && !upKeyIsDown)
{
player_mc.gotoAndStop(1);
}
if(rightKeyIsDown)
{
player_mc.gotoAndStop(2);
player_mc.x+= playerSpeed;
player_mc.scaleX = 0.59;
}
if(leftKeyIsDown)
{
player_mc.gotoAndStop(2);
player_mc.x-= playerSpeed;
player_mc.scaleX = -0.59;
}
if(upKeyIsDown && canJump)
{
player_mc.gotoAndStop(3);
yVelocity = -15;
canJump = false;
canDoubleJump = true;
}
if(upKeyIsDown && canDoubleJump && yVelocity > -2)
{
yVelocity = -13;
canDoubleJump = false;
}
yVelocity +=gravity;
if(!floor_mc.hitTestPoint(player_mc.x,player_mc.y, true))
{
player_mc.y+=yVelocity;
}
if(yVelocity > 20)
{
yVelocity =20;
}
for(var i:int=0; i<10; i++)
{
if(floor_mc.hitTestPoint(player_mc.x, player_mc.y, true))
{
player_mc.y--;
yVelocity = 0;
canJump = true;
}
}
for(var j:int=0; j<=2; j++)
{
if(rb.hitTestPoint(player_mc.x, player_mc.y, true))
{
player_mc.x = -1703.35;
player_mc.y = 322.1;
player_mc.scaleX = 0.59;
lives = lives - 1;
}
if(lives == 0)
{
// GameOver();
// remove all the event listeners
// stage.removeEventListener(Event.ENTER_FRAME, GameOver);
stage.removeEventListener(Event.ENTER_FRAME, movePlayer);
stage.removeEventListener(KeyboardEvent.KEY_DOWN, PressAKey);
stage.removeEventListener(KeyboardEvent.KEY_UP, ReleaseAKey);
stage.removeEventListener(Event.ENTER_FRAME, cameraFollowCharacter);
gotoAndStop(124);
//(root as MovieClip).gotoAndStop(124);
}
}
// appleCount_txt.text = "Apples:" + appleCount;
}
/*function GameOver()
{
// lives = 3;
// remove all the event listeners
stage.removeEventListener(Event.ENTER_FRAME, GameOver);
stage.removeEventListener(Event.ENTER_FRAME, movePlayer);
stage.removeEventListener(KeyboardEvent.KEY_DOWN, PressAKey);
stage.removeEventListener(KeyboardEvent.KEY_UP, ReleaseAKey);
stage.removeEventListener(Event.ENTER_FRAME, cameraFollowCharacter);
// player_mc.stop();
gotoAndStop(1); // this has your "dead" screen on it.
}*/
Can anyone help me solving this problem please?
Thanks
There's a listener still getting executed after the frame change. In that handler, an object is referenced that does not exist on that frame and is thus null.
Changing frames only provides visual changes, not necessarily a complete change of state.

A conflict exists with definition leftIdle1 in namespace internal

I'm scripting a game with Actionscript 3.0 in Flash Professional CS5.5, but I get an error.
Scene 1, Layer 'as2', Frame 153, Line 4 1151: A conflict exists with definition leftIdle1 in namespace internal.
(I get these with the other Variables too.)
Now it's a platform game, and I am going to put cutscenes throughout in the game and I need to switch from frames and put the code in another frame. But it gives that error, I turned off the 'Automatic Declare stage instances' function, now I checked this website and Googled it, people get it with their Movieclips, I get it with my variables.
This is my script:
var leftKeyDown1:Boolean = false;
var rightKeyDown1:Boolean = false;
var spaceKeyDown1:Boolean = false;
var leftIdle1:Boolean = false;
var rightIdle1:Boolean = true;
var mainSpeed1:Number = 4;
player.addEventListener(Event.ENTER_FRAME, moveChar);
function moveChar(event:Event)
{
if (leftKeyDown1)
{
player.x -= mainSpeed1;
leftIdle1 = true;
rightIdle1 = false;
player.gotoAndStop("walk_left");
}
if (rightKeyDown1)
{
player.x += mainSpeed1;
rightIdle1 = true;
leftIdle1 = false;
player.gotoAndStop("walk_right");
}
if (rightIdle1 && !rightKeyDown1 && !leftKeyDown1)
{
player.gotoAndStop("idle_right");
}
else if (leftIdle1 && !rightKeyDown1 && !leftKeyDown1)
{
player.gotoAndStop("idle_left");
}
if (collide.hitTestObject(player))
{
player.x = player.x + mainSpeed1;
}
if (trigger1.hitTestObject(player))
{
son1.gotoAndStop("walkRight");
trigger1.gotoAndStop(2);
son1.x += 2;
}
if (trigger2.hitTestObject(player))
{
gotoAndPlay(4);
}
}
stage.addEventListener(KeyboardEvent.KEY_DOWN, checkKeysDown);
function checkKeysDown(event:KeyboardEvent)
{
if (event.keyCode == 37 || event.keyCode == 65)
{
leftKeyDown1 = true;
}
if (event.keyCode == 39 || event.keyCode == 68)
{
rightKeyDown1 = true;
}
if (event.keyCode == 32)
{
spaceKeyDown1 = true;
}
}
stage.addEventListener(KeyboardEvent.KEY_UP, checkKeysU1);
function checkKeysUp(event:KeyboardEvent)
{
if (event.keyCode == 37 || event.keyCode == 65)
{
leftKeyDown1 = false;
}
if (event.keyCode == 32)
{
spaceKeyDown1 = false;
}
if (event.keyCode == 39 || event.keyCode == 68)
{
rightKeyDown1 = false;
}
}
It's probably coded in a weird way, but whatever.
I have no idea what to do at this point.
Help would be really appreciated.
EDIT:
Oh I got another error too. It's with Duplicate function, and I can't seem to fix it but to rename those, and it will take a long time to rename them every-time. So if someone has something for that, thanks!
you have duplicated definitions.
For example:
var leftIdle1:Boolean
exists multiple times - remove the duplicates

Adding a single instance of an object to the stage

Good Evening,
I can't seem to get my code to only add one instance of an object to the stage. It seems to add atleast 3 instances of the object to the stage. The objects added is r2, r3 and r4.
stop();
import flash.events.Event;
stage.focus=stage;
var upKeyDown5:Boolean = false;
var rightKeyDown5:Boolean = false;
var downKeyDown5:Boolean = false;
var leftKeyDown5:Boolean = false;
var enterkey:Boolean = false;
var interaction:Boolean = false;
p5.addEventListener(Event.ENTER_FRAME, moveChar5);
stage.addEventListener(KeyboardEvent.KEY_DOWN, checkKeysDown5);
stage.addEventListener(KeyboardEvent.KEY_UP, checkKeysUp5);
Mouse.hide();
function moveChar5(e:Event):void{
if(downKeyDown5 && !upKeyDown5 && !rightKeyDown5 && !leftKeyDown5)
{
p5.gotoAndStop("walk_down");
if(p5.y < 492.75)
p5.y += 6;
}
if(upKeyDown5 && !downKeyDown5 && !rightKeyDown5 && !leftKeyDown5)
{
p5.gotoAndStop("walk_up");
if(p5.y > 202.85)
p5.y -= 6;
}
if(rightKeyDown5 && !upKeyDown5 && !downKeyDown5 && !leftKeyDown5)
{
p5.gotoAndStop("walk_right");
if(p5.x < 871.5)
p5.x += 6;
}
if(leftKeyDown5 && !upKeyDown5 && !rightKeyDown5 && !downKeyDown5)
{
p5.gotoAndStop("walk_left");
if(p5.x > 203.65)
p5.x -= 6;
}
if(enterkey && interaction && p5.hitTestObject(c1)){
if (!(Boolean(stage.getChildByName('r2')))) {
var rat2:r2;
rat2 = new r2();
addChild(rat2);
rat2.y=38;
rat2.x=32;
}
}
if(enterkey && interaction && p5.hitTestObject(c2)){
if (!(Boolean(stage.getChildByName('r3')))) {
var rat3:r3;
rat3 = new r3();
addChild(rat3);
rat3.y=38;
rat3.x=32;
}
}
if(enterkey && interaction && p5.hitTestObject(c3)){
if (!(Boolean(stage.getChildByName('r4')))) {
var rat4:r4;
rat4 = new r4();
addChild(rat4);
rat4.y=38;
rat4.x=32;
}
}
}
function checkKeysDown5(event:KeyboardEvent):void{
if(event.keyCode == 87){
upKeyDown5 = true;
}
if(event.keyCode == 68){
rightKeyDown5 = true;
}
if(event.keyCode == 83){
downKeyDown5 = true;
}
if(event.keyCode == 65){
leftKeyDown5 = true;
}
if (event.keyCode == 13){
enterkey = true;
}
}
function checkKeysUp5(event:KeyboardEvent):void{
if(event.keyCode == 87){
upKeyDown5 = false;
p5.gotoAndStop("still_up");
}
if(event.keyCode == 68){
rightKeyDown5 = false;
p5.gotoAndStop("still_right");
}
if(event.keyCode == 65){
leftKeyDown5 = false;
p5.gotoAndStop("still_left");
}
if(event.keyCode == 83){
downKeyDown5 = false;
p5.gotoAndStop("still_down");
}
if (event.keyCode == 13){
enterkey = false;
}
if(p5.hitTestObject(c1) || p5.hitTestObject(c2) || p5.hitTestObject(c3)){
p5.gotoAndStop("interaction");
interaction = true;
}
else
interaction = false;
}
Thanks in advance
It would be more efficient and cleaner to use a switch statement instead all those if statements (if you only ever want one and only one of those conditions to be met)
You're issue I think has to do with the way you're tracking your rats - eg. using the stage.getChildByName.
Below is an example, plus some commented notes on some of your other potential issues.
switch(true){
case (enterkey && interaction && p5.hitTestObject(c1)):
if (!(Boolean(stage.getChildByName('r2')))) { //your not giving your rat a name so this will always return false, plus you're not adding the rat to the stage but to this class
var rat2:r2;
rat2 = new r2();
addChild(rat2); //use stage.addChild if you want the above check (stage.getChildByName) to work
rat2.y=38;
rat2.x=32;
rat2.name = "r2"; //if you want the above check to work
break; //break out of the switch if you don't want any of the others evaluate since this rat got added
}
//do a case for your other blocks
}
It would be better to not use stage.getChidByName at all, and instead create a method like this:
function checkRatExists(ratClass:Class):Boolean {
var tmp:DisplayObject;
var i:int = numChildren;
while(i--){
tmp = getChildAt(i);
if(tmp is ratClass){
return true;
}
}
return false;
}
Then use that new function instead of stage.getChildByName:
if (!checkRatExists(r2)) //r2 is the class of rat you want to check
As an aside from your issue, it would be much cleaner to create a method/function to do your rat positioning and adding instead of duplicated the code over and over and over...
function addRat(rat:RatCommonBaseClass, ratName:String):void {
addChild(rat);
rat.y=38;
rat.x=32;
rat.name = ratName;
}
//now you can just do this for all your rats:
addRat(new r2(),"r2);
You don't have an else condition for the your statements where you add the objects, so most likely what is happening is that all the statements are satisfying the condition at once. Adding an else statement should prevent this from happening:
if(enterkey && interaction && p5.hitTestObject(c1)){
if (!(Boolean(stage.getChildByName('r2')))) {
var rat2:r2;
rat2 = new r2();
addChild(rat2);
rat2.y=38;
rat2.x=32;
}
}
else if(enterkey && interaction && p5.hitTestObject(c2)){
if (!(Boolean(stage.getChildByName('r3')))) {
var rat3:r3;
rat3 = new r3();
addChild(rat3);
rat3.y=38;
rat3.x=32;
}
}
else if(enterkey && interaction && p5.hitTestObject(c3)){
if (!(Boolean(stage.getChildByName('r4')))) {
var rat4:r4;
rat4 = new r4();
addChild(rat4);
rat4.y=38;
rat4.x=32;
}
}
This way it will first check to see if p5 has hit c1 and only if it hasn't will it check if it has hit c2 and then the same thing for c3.

how do I either stop the tween at the point of my choice (first code)? or how do I stop the easing (second code)?

This is for Actionscript 3.0 on CS5.
I've been having some issues with fl.transitions.easing.None.easeNone. I tried to animate my MovieClip character without easing and stopping on a dime. I used the following code to stop the easing:
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
function keyPressed(evt:KeyboardEvent):void
{
if(evt.keyCode == Keyboard.RIGHT && var_move == false)
{
var_move = true;
hero.gotoAndStop("Walk");
hero.scaleX = 1;
var tween1:Tween = new Tween(hero, "x", None.easeNone, hero.x, hero.x+75, 15, false)
tween1.addEventListener(TweenEvent.MOTION_FINISH, onMotionFinished);
}
if(evt.keyCode == Keyboard.LEFT && var_move == false)
{
var_move = true;
hero.gotoAndPlay("Walk");
hero.scaleX = -1;
var tween2:Tween = new Tween(hero, "x", None.easeNone, hero.x, hero.x-75, 15, false)
tween2.addEventListener(TweenEvent.MOTION_FINISH, onMotionFinished);
}
}
function onMotionFinished($evt:TweenEvent):void
{
hero.gotoAndPlay("Stand");
var_move = false;
}
Which it did but if you just tap Right or Left Arrow the MovieClip character will not stop on that frame. It continues until the tween is done. If I use the following code:
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, releaseKey);
function keyPressed(evt:KeyboardEvent):void
{
if(evt.keyCode == Keyboard.RIGHT)
{
var_move = true;
hero.gotoAndPlay("Walk");
hero.scaleX = 1;
hero.x += 5;
}
if(evt.keyCode == Keyboard.LEFT)
{
var_move = true;
hero.gotoAndPlay("Walk");
hero.scaleX = -1;
hero.x -= 5;
}
}
function releaseKey(evt:KeyboardEvent):void
{
if(evt.keyCode == Keyboard.RIGHT || evt.keyCode == Keyboard.LEFT)
{
hero.gotoAndStop("Stand");
}
}
the MovieClip character eases into the walk animation, but stops where I want it to. how do I either stop the tween at the point of my choice (first code) or how do I stop the easing (second code)?
For tweening, I highly recommend using the library at http://greensock.com ... the free TweenMax/TweenLite will save you tons of time trying to solve problems like this... they already did the hard part :)