AS3 gotoAndStop and if-else-if - actionscript-3

i am trying to make a rotational view of a block in 6 frames with 1-4 being the facing 4 sides, and 5-6 being top and bottom. i have 4 arrows to "rotate" this view.
i am able to rotate 1-6 but unable to rotate from 5 & 6 back to 1-4.
this is my code. i am bad with AS, so pls be merciful with my mistakes..
btn_view_right.addEventListener(MouseEvent.CLICK, viewRight);
function viewRight(event: MouseEvent): void {
if (currentFrame == 1) {
gotoAndStop(2);
} else if (currentFrame == 4) {
gotoAndStop(1);
} else if (currentFrame == 5) {
gotoAndStop(6);
} else if (currentFrame == 6) {
gotoAndStop(5);
} else {
nextFrame();
}
}
btn_view_top.addEventListener(MouseEvent.CLICK, viewTop);
function viewTop(event: MouseEvent): void {
if (currentFrame == 1, 2, 3, 4) {
gotoAndStop(5);
} else if (currentFrame == 5, 6) {
gotoAndStop(1);
}
}

I think the sintax that you use in the second function is not correct, you have to explictly use the or (||) operator.
try this code:
stop();
btn_view_right.addEventListener(MouseEvent.CLICK, viewRight);
function viewRight(event: MouseEvent): void {
if (currentFrame == 1) {
gotoAndStop(2);
} else if (currentFrame == 4) {
gotoAndStop(1);
} else if (currentFrame == 5) {
gotoAndStop(6);
} else if (currentFrame == 6) {
gotoAndStop(5);
} else {
nextFrame();
}
}
btn_view_top.addEventListener(MouseEvent.CLICK, viewTop);
function viewTop(event: MouseEvent): void {
if (currentFrame == 1 || currentFrame == 2 || currentFrame == 3 || currentFrame == 4) {
gotoAndStop(5);
} else if (currentFrame == 5 || currentFrame == 6) {
gotoAndStop(1);
}
}

Related

AS3 hitTestObject is not working

I was making flash that moving a circle on my keyboard and creating a flash that hit the wall.
But method 'hitTestObject' is not working...
Hou to solve this ploblem?
import flash.events.KeyboardEvent;
import flash.events.Event;
stage.addEventListener(KeyboardEvent.KEY_DOWN,atKeydown);
stage.addEventListener(KeyboardEvent.KEY_UP,atKeyup);
function atKeydown(e :KeyboardEvent) :void
{
if(e.keyCode == 38) stage.addEventListener(Event.ENTER_FRAME,enterYUp);
if(e.keyCode == 40) stage.addEventListener(Event.ENTER_FRAME,enterYDown);
if(e.keyCode == 37) stage.addEventListener(Event.ENTER_FRAME,enterXLeft);
if(e.keyCode == 39) stage.addEventListener(Event.ENTER_FRAME,enterXRight);
}
function atKeyup(e :KeyboardEvent) :void
{
if(e.keyCode == 38) stage.removeEventListener(Event.ENTER_FRAME,enterYUp);
if(e.keyCode == 40) stage.removeEventListener(Event.ENTER_FRAME,enterYDown);
if(e.keyCode == 37) stage.removeEventListener(Event.ENTER_FRAME,enterXLeft);
if(e.keyCode == 39) stage.removeEventListener(Event.ENTER_FRAME,enterXRight);
}
function enterYUp(e :Event) :void
{
ball.y -= 10;
}
function enterYDown(e :Event) :void
{
ball.y += 10;
}
function enterXLeft(e :Event) :void
{
ball.x -= 10;
}
function enterXRight(e :Event) :void
{
ball.x += 10;
}
if(ball.hitTestObject(wall) == true) trace("!!!!");
function enterYUp(e :Event) :void
{
var bool = checkHit("y", -10); // each frame checks for a hit, returns a Boolean (see the function below)
carryOut(bool, "y", -10); // the movement is carried out based on the Boolean
}
function enterYDown(e :Event) :void
{
var bool = checkHit("y", +10); // you can send the "y" property, and where it wants to go.
carryOut(bool, "y", +10);
}
function enterXLeft(e :Event) :void
{
var bool = checkHit("x", -10);
carryOut(bool, "x", -10);
}
function enterXRight(e :Event) :void
{
var bool = checkHit("x", +10);
carryOut(bool, "x", -10);
}
function checkHit(_prop, _num):Boolean
{
// This single function will run every frame, comparing the balls 'predicted position'
if((ball[_prop] + _num) <= _numCheck)
{
// i might use hitTestPoint instead, but I hate using hitTestObject... it can act weird sometimes.
return true;
}
else
{
return false;
}
}
function carryOut(bool, _prop,_num):void
{
if(bool)
{
trace("hit");
}
else
{
// if the Boolean returned false
// ball[_prop] is ball.x or ball.y depending on if you sent "x" or "y".
// it will ADD the _num you sent, so adding -10 is the same as subtracting.
ball[_prop] += _num;
}
}

Flash Actionscript Game - Character Not Moving after Adding New Keyframes

I am really new to actionscript and am having an issue with a project I am working on for school. The code that makes my character move (Right, Left, Up, Down) all worked fine but then I added a new Keyframe at the beginning of the project and created a scene to choose a character. I have set it up exactly like my teacher showed us but now that the new keyframe is there, I click the character and it goes to the gameplay but the character cannot be moved. Any help would be very much appreciated.
Here is the code from the first key frame:
stop ();
mc_fox.visible = false;
import flash.events.Event;
import flash.events.MouseEvent;
btn_fox.addEventListener(MouseEvent.CLICK,btn_foxHandler);
function btn_foxHandler(event:MouseEvent):void
{
gotoAndStop(2);
}
And The code from frame 2:
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
mc_fox.stop ();
var move:uint = 0;
stage.addEventListener (KeyboardEvent.KEY_DOWN, keydownHandler);
stage.addEventListener (KeyboardEvent.KEY_UP, keyupHandler);
function keyupHandler (event:KeyboardEvent) :void {
if (event.keyCode == Keyboard.RIGHT){
move = 0
mc_fox.gotoAndStop (1);
}
else if (event.keyCode == Keyboard.LEFT){
move = 0
mc_fox.gotoAndStop (15);
}
else if (event.keyCode == Keyboard.UP){
move = 0
mc_fox.gotoAndStop (30);
}
else if (event.keyCode == Keyboard.DOWN){
move = 0
mc_fox.gotoAndStop (45);
}
}
function keydownHandler (event:KeyboardEvent) :void {
if(event.keyCode == Keyboard.RIGHT && mc_fox.x < 889) {
if (move ==0){
mc_fox.gotoAndPlay (1);
move = 1
}
else {
mc_fox.x = mc_fox.x + 5;
mc_fox.play ();
}
}
else if(event.keyCode == Keyboard.LEFT && mc_fox.x > 111) {
if (move ==0){
mc_fox.gotoAndPlay (15);
move = 1
}
else {mc_fox.x = mc_fox.x - 5;
mc_fox.play ();
}
}
else if (event.keyCode == Keyboard.UP && mc_fox.y > 270) {
if (move ==0){
mc_fox.gotoAndPlay (30);
move = 1
}
else{mc_fox.y = mc_fox.y - 5;
mc_fox.width = mc_fox.width - .9;
mc_fox.height = mc_fox.height - .9;
mc_fox.play();
}
}
else if (event.keyCode == Keyboard.DOWN) {
if (move ==0){
mc_fox.gotoAndPlay (45);
move = 1
}
else{mc_fox.y = mc_fox.y + 5;
mc_fox.width = mc_fox.width + .9;
mc_fox.height = mc_fox.height + .9;
mc_fox.play();
}
}
}

as3 - how to stop animation when both keys pressed?

My player stops moving when two keys are pressed at the same time. But the animation still moves. For example, if I press up and down at the same time or right and left at the same time.
On key down event listener:
if (event.keyCode == Keyboard.D)
{
isRight = true
}
if (event.keyCode == Keyboard.A)
{
isLeft = true
}
if (event.keyCode == Keyboard.W)
{
isUp = true
}
if (event.keyCode == Keyboard.S)
{
isDown = true
}
On key up event listener:
if (event.keyCode == Keyboard.D)
{
isRight = false
gotoAndStop(1);
}
if (event.keyCode == Keyboard.A)
{
isLeft = false
gotoAndStop(1);
}
if (event.keyCode == Keyboard.W)
{
isUp = false
gotoAndStop(1);
}
if (event.keyCode == Keyboard.S)
{
isDown = false
gotoAndStop(1);
}
On enterframe:
if (isRight == true)
{
x += 5;
play();
}
if (isLeft == true )
{
x -= 5;
play();
}
if (isUp == true)
{
y -= 5;
play();
}
if (isDown == true)
{
y += 5;
play();
}
If players goes x -= 1 and x += 1 it basically moves x += 0 overall. We can easily check that and stop animation in needed:
var iP:Point = new Point(x,y);//try to avoid creating new objects on frame interval
if (isRight) x += 5;
if (isLeft) x -= 5;
if (isUp) y -= 5;
if (isDown) y += 5;
if(!Point.distance(iP,new Point(x,y)) goToAndStop(1);
else play();
I don't see any checks to see if more than 1 key is being pressed?
surely you should introduce something like a keycount into the enterframe:
var count:uint = 0;
if (isRight == true){
count++
x += 5;
}
if (isLeft == true ){
count++;
x -= 5;
}
if (isUp == true){
count++;
y -= 5;
}
if (isDown == true){
count++
y += 5;
}
if (count > 1) {
isRight = isLeft = isUp = isDown = false;
gotoAndStop(1);
} else {
play();
}

Collision code stops working after adding to stage

So I'm using actionscript 3, and my collision code stops working after adding it to stage with code. the player just cant move at all when touching the wall. However if I add it to stage by dragging the movieclip to the stage, it works fine, the player doesn't freeze when touching the wall, instead it cant pass the wall but it can move back from it.
this is my code that adds it to stage.
public class Main extends MovieClip
{
var mountains: Mountains;
var homePage: HomePage;
var oneManager: OneManager;
public function Main()
{
mountains = new Mountains;
homePage = new HomePage;
oneManager = new OneManager;
addChild(homePage);
stage.displayState = "fullScreen";
homePage.playButtons.addEventListener(MouseEvent.CLICK, onPlayButtonsClick);
}
function onPlayButtonsClick(event:MouseEvent):void
{
stage.focus = stage;
parent.addChild(oneManager);
homePage.parent.removeChild(homePage);
}
}
this is my code that uses the collision detection to stop the player from crossing the wall
private function onKeyDown(event: KeyboardEvent): void
{
if (event.keyCode == Keyboard.A)
{
vx = -5;
}
else if (event.keyCode == Keyboard.D)
{
vx = 5;
}
else if (event.keyCode == Keyboard.W)
{
vy = -5;
}
else if (event.keyCode == Keyboard.S)
{
vy = 5;
}
}
private function onKeyUp(event: KeyboardEvent): void
{
if (event.keyCode == Keyboard.A || event.keyCode == Keyboard.D)
{
vx = 0;
}
else if (event.keyCode == Keyboard.S || event.keyCode == Keyboard.W)
{
vy = 0;
}
}
private function onEnterFrame(event: Event): void
{
if (player.collisionArea.hitTestObject(wall))
{
player.y -= vy;
}
}

Some Problems with my AS3 code

Some troubles to make my new flash game(i used AS3,CS5.5):
I just try to make my character(hatt) walk and jump, but I can't make it at the same time, well, idk how... Furthermore, I don't know how make my character recognize the ground.
And at last this thing here:
"Scene 1, Layer 'hatt', Frame 1, Line 6 Warning: 1090: Migration issue: The onKeyDown event handler is not triggered automatically by Flash Player at run time in ActionScript 3.0. You must first register this handler for the event using addEventListener ( 'keyDown', callback_handler)."
Plz help me and give some hints plz... Thanks.
Here's the code:
var grav:Number = 10;
var jumping:Boolean = false;
var jumpPow:Number = 0;
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
stage.addEventListener(Event.ENTER_FRAME, update);
function onKeyDown(evt:KeyboardEvent):void
{
if (evt.keyCode == Keyboard.UP)
{
if (jumping != true)
{
jumpPow = -25;
jumping = true;
}
}
}
function update(evt:Event):void
{
if (jumping)
{
hatt.y += jumpPow;
jumpPow += grav;
if (hatt.y >= stage.stageHeight)
{
jumping = false;
hatt.y = stage.stageHeight;
}
}
}
stage.addEventListener (KeyboardEvent.KEY_DOWN, myFunction) ;
function myFunction (event: KeyboardEvent){
if(event.keyCode == Keyboard.LEFT) {
hatt.x -= 5
}
if(event.keyCode == Keyboard.RIGHT) {
hatt.x += 5
}
}
remove this line:
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
remove function onKeyDown:
function onKeyDown(evt:KeyboardEvent):void
{
if (evt.keyCode == Keyboard.UP)
{
if (jumping != true)
{
jumpPow = -25;
jumping = true;
}
}
}
replace myFunction with this:
function myFunction (event: KeyboardEvent)
{
if(event.keyCode == Keyboard.LEFT)
hatt.x -= 5;
if(event.keyCode == Keyboard.RIGHT)
hatt.x += 5;
if(event.keyCode == Keyboard.UP && jumping != true)
{
jumpPow = -25;
jumping = true;
}
}