I'm using flash CS4 to have an animated character moving within the stage boundaries. There are no code errors when generating the SWF file and the character is moving fine when I test the movie in Flash. Once I publish the file, the character is completely unresponsive to the arrow keys. I have no clue as what the issue might be and some help would be greatly appreciated. Here's the code of the movie :
stop();
var upPressed:Boolean = false;
var downPressed:Boolean = false;
var leftPressed:Boolean = false;
var rightPressed:Boolean = false;
var isWalking:Boolean = false;
var mySpeed:Number = 3;
my_Sprite.addEventListener(Event.ENTER_FRAME, moveSprite);
stage.addEventListener(KeyboardEvent.KEY_DOWN, setKeyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, unsetKeyPressed);
function moveSprite(event:Event):void {
if(upPressed && my_Sprite.y >= 40){
my_Sprite.y -= mySpeed;
}
if(downPressed && my_Sprite.y <= 440){
my_Sprite.y += mySpeed;
}
if(leftPressed && my_Sprite.x >= 20){
my_Sprite.x -= mySpeed;
}
if(rightPressed && my_Sprite.x <= 600){
my_Sprite.x += mySpeed;
}
}
function setKeyPressed(event:KeyboardEvent):void {
switch(event.keyCode){
case Keyboard.UP: {
upPressed = true;
if (!isWalking) {
my_Sprite.gotoAndPlay("Up");
isWalking = true;
}
break;
}
case Keyboard.DOWN: {
downPressed = true;
if (!isWalking) {
my_Sprite.gotoAndPlay("Down");
isWalking = true;
}
break;
}
case Keyboard.LEFT: {
leftPressed = true;
if (!isWalking) {
my_Sprite.gotoAndPlay("Left");
isWalking = true;
}
break;
}
case Keyboard.RIGHT: {
rightPressed = true;
if (!isWalking) {
my_Sprite.gotoAndPlay("Right");
isWalking = true;
}
break;
}
}
}
function unsetKeyPressed(event:KeyboardEvent):void {
switch(event.keyCode) {
case Keyboard.UP: {
upPressed = false;
my_Sprite.gotoAndStop("Up");
isWalking = false;
break;
}
case Keyboard.DOWN: {
downPressed = false;
my_Sprite.gotoAndStop("Down");
isWalking = false;
break;
}
case Keyboard.LEFT: {
leftPressed = false;
my_Sprite.gotoAndStop("Left");
isWalking = false;
break;
}
case Keyboard.RIGHT: {
rightPressed = false;
my_Sprite.gotoAndStop("Right");
isWalking = false;
break;
}
}
}
You may need to add some JavaScript to your HTML to set the focus on the Flash object:
<body onLoad="window.document.movieID.focus();">
Make sure you change movieID to whatever you actually set as your ID.
you can force the focus by doing
this.stage.focus = myTextField; // assuming this is added to the display hierarchy
Related
I am trying to make a fishing game where each fish you catch gives you a point and different enemies subtract points. I have found a tutorial for this in ActionScript 2 but I can't find any for action script 3 (here is the link to what I want to replicate: http://www.flashkit.com/tutorials/Games/Creating-Imtiazur-1297/index.php). I am relatively new at action script as I have only made animations before but I was able to do a little but then couldn't figure out the rest.
I currently have the fishing line moving up and down and does not go beyond the fishing line but I'm not sure how to make the line get longer when it goes down. At the moment the whole line and hook move with a space between it and the rod. I also have to click before the line moves (I'm not sure how to make it instantly usable when going to the frame). I also have one fish moving across the screen. I don't know how to make multiple of the same fish or enemies going across the screen at the same time.
Any help would be greatly appreciated​!
fish.addEventListener(Event.ENTER_FRAME, fl_AnimateHorizontally);
function fl_AnimateHorizontally(event:Event)
{
fish.x = fish.x + 5; if (fish.x>1100)
{
fish.x = -100;
}
shark.x = shark.x + 7; if (shark.x>3500)
{
shark.x = -400;
}
jellyfish.x = jellyfish.x + 3; if (jellyfish.x>3000)
{
jellyfish.x = -100;
}
boot.x = boot.x + 5; if (boot.x>1500)
{
boot.x = -100;
}
barrel.x = barrel.x + 4; if (barrel.x>1600)
{
barrel.x = -100;
}
}
var upPressed:Boolean = false;
var downPressed:Boolean = false;
var leftPressed:Boolean = false;
var rightPressed:Boolean = false;
rope.addEventListener(Event.ENTER_FRAME, fl_MoveInDirectionOfKey);
stage.addEventListener(KeyboardEvent.KEY_DOWN, fl_SetKeyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, fl_UnsetKeyPressed);
function fl_MoveInDirectionOfKey(event:Event)
{
if (upPressed)
{
rope.y -= 10; hook.y -= 10;
}
if (downPressed)
{
rope.y += 10; hook.y += 10;
}
if (leftPressed)
{
rope.x -= 0; hook.x -= 0;
}
if (rightPressed)
{
rope.x += 0; hook.x += 0;
}
if (rope.y < 35)
{
rope.y = 35; hook.y = 100;
}
}
function fl_SetKeyPressed(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.UP:
{
upPressed = true;
break;
}
case Keyboard.DOWN:
{
downPressed = true;
break;
}
case Keyboard.LEFT:
{
leftPressed = true;
break;
}
case Keyboard.RIGHT:
{
rightPressed = true;
break;
}
}
}
function fl_UnsetKeyPressed(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.UP:
{
upPressed = false;
break;
}
case Keyboard.DOWN:
{
downPressed = false;
break;
}
case Keyboard.LEFT:
{
leftPressed = false;
break;
}
case Keyboard.RIGHT:
{
rightPressed = false;
break;
}
}
}
I want to make a moving character that runs a movie clip when a keyboard arrow is pressed.For example:
When no arrows are pressed I want the character not to move when any keyboard arrows are pressed and run a movie clip animation where the character runs when the right arrow is pressed and same with the left arrow.
var upPressed:Boolean = false;
var downPressed:Boolean = false;
var leftPressed:Boolean = false;
var rightPressed:Boolean = false;
movieClip_1.addEventListener(Event.ENTER_FRAME,fl_MoveInDirectionOfKey);
stage.addEventListener(KeyboardEvent.KEY_DOWN, fl_SetKeyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, fl_UnsetKeyPressed);
function fl_MoveInDirectionOfKey(event:Event)
{
if (upPressed)
{
movieClip_1.y -= 5;
}
if (downPressed)
{
movieClip_1.y += 5;
}
if (leftPressed)
{
movieClip_1.x -= 5;
}
if (rightPressed)
{
movieClip_1.x += 5;
}
}
function fl_SetKeyPressed(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.UP:
{
upPressed = true;
break;
}
case Keyboard.DOWN:
{
downPressed = true;
break;
}
case Keyboard.LEFT:
{
leftPressed = true;
break;
}
case Keyboard.RIGHT:
{
rightPressed = true;
break;
}
}
}
function fl_UnsetKeyPressed(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.UP:
{
upPressed = false;
break;
}
case Keyboard.DOWN:
{
downPressed = false;
break;
}
case Keyboard.LEFT:
{
leftPressed = false;
break;
}
case Keyboard.RIGHT:
{
rightPressed = false;
break;
}
}
}
My movie clips are: run_right and run_left.
My timeline:
Create a MovieClip called "movieClip_1".
var movieClip_1= new MovieClip_1();
Inside this MovieClip, add "run_right" animation on the first frame and "run_left" on the second frame.
I mean, add new MovieClips that contains your animations.
Then, go to fl_MoveInDirectionOfKey function and write this:
if (rightPressed)
{
movieClip_1.gotoAndStop(1);
}
else if (leftPressed)
{
movieClip_1.gotoAndStop(2);
}
else
{
// no animation
// movieClip_1.gotoAndStop(3); idle animation could be on frame 3
}
here is your Mistake
movieClip_1.addEventListener(Event.ENTER_FRAME,fl_MoveInDirectionOfKey);
------------
remove
movieClip_1.
it should be like this
stage.addEventListener(Event.ENTER_FRAME,fl_MoveInDirectionOfKey);
This is second solution, answer ( the Event.Enter_Frame ) is inside movieClip so you need to add (root.) before MovieClip
change it like this
if (upPressed)
{
root.movieClip_1.y -= 5;
}
if (downPressed)
{
root.movieClip_1.y += 5;
}
if (leftPressed)
{
root.movieClip_1.x -= 5;
}
if (rightPressed)
{
root.movieClip_1.x += 5;
}
I have a 2D pokemon-style game where I am trying to create a wall that the trainer should not be able to move through (the trees). How would I use hitTestObject to test if the character is touching the tree and then make the character not able to move in that direction?
var upPressed:Boolean = false;
var downPressed:Boolean = false;
var leftPressed:Boolean = false;
var rightPressed:Boolean = false;
var facingDown:Boolean = false;
var noPressedU:Boolean = false;
var noPressedD:Boolean = false;
var noPressedL:Boolean = false;
var noPressedR:Boolean = false;
char.addEventListener(Event.ENTER_FRAME, fl_MoveInDirectionOfKey_2);
stage.addEventListener(KeyboardEvent.KEY_DOWN, fl_SetKeyPressed_2);
stage.addEventListener(KeyboardEvent.KEY_UP, fl_UnsetKeyPressed_2);
this.addEventListener( Event.ENTER_FRAME, handleCollision);
function fl_MoveInDirectionOfKey_2(event:Event)
{
if (upPressed && rightPressed)
{
char.y -= 5;
char.x += 5;
char.gotoAndPlay("mright");
}
else if (upPressed && leftPressed)
{
char.y -= 5;
char.x -= 5;
char.gotoAndPlay("mleft");
}
else if (upPressed)
{
char.y -= 5;
char.gotoAndPlay("mup");
}
else if (downPressed && rightPressed)
{
char.y += 5;
char.x += 5;
char.gotoAndPlay("mright");
}
else if (downPressed && leftPressed)
{
char.y += 5;
char.x -= 5;
char.gotoAndPlay("mleft");
}
else if (downPressed)
{
char.y += 5;
char.gotoAndPlay("mdown");
}
else if (leftPressed)
{
char.gotoAndPlay("mleft");
char.x -= 5;
}
else if (rightPressed)
{
char.gotoAndPlay("mright");
char.x += 5;
}
}
function fl_SetKeyPressed_2(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.UP :
{
upPressed = true;
break;
};
case Keyboard.DOWN :
{
downPressed = true;
break;
};
case Keyboard.LEFT :
{
leftPressed = true;
break;
};
case Keyboard.RIGHT :
{
rightPressed = true;
break;
}
}
}
function fl_UnsetKeyPressed_2(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.UP :
{
upPressed = false;
noPressedU = true;
lastPressed = 1;
break;
};
case Keyboard.DOWN :
{
downPressed = false;
noPressedD = true;
lastPressed = 2;
break;
};
case Keyboard.LEFT :
{
leftPressed = false;
noPressedL = true;
lastPressed = 3;
break;
};
case Keyboard.RIGHT :
{
rightPressed = false;
noPressedR = true;
lastPressed = 4;
break;
};
case Keyboard.RIGHT && Keyboard.UP :
{
rightPressed = false;
break;
}
}
}
Pardon the messy coding. ;-;
I'm learning AS3 using Adobe Flash CS6, I'm working on a tutorial to create a moving object with animation using KeyboardEvents and it works perfectly but now I want to change it so I can use buttons on the screen (MouseEvents) to move the character but I'm not sure how to go about it.
var movingRight;
var movingLeft;
function onKeyPress(e:KeyboardEvent):void
{
if(e.keyCode == Keyboard.RIGHT){
movingRight=1;
}
else if(e.keyCode == Keyboard.LEFT){
movingLeft=1;
}
}
To move your character using buttons you have to use two MouseEvents : a MouseEvent.MOUSE_DOWN event when a button is pressed and MouseEvent.MOUSE_UP when it's released, so you can do like this :
LeftBTN.addEventListener(MouseEvent.MOUSE_DOWN, LeftBTN_onPress);
LeftBTN.addEventListener(MouseEvent.MOUSE_UP, LeftBTN_onRelease);
function LeftBTN_onPress(e:MouseEvent):void
{
movingLeft = 1;
}
function LeftBTN_onRelease(e:MouseEvent):void
{
character.gotoAndStop(1);
movingLeft = 0;
}
RightBTN.addEventListener(MouseEvent.MOUSE_DOWN, RightBTN_onPress);
RightBTN.addEventListener(MouseEvent.MOUSE_UP, RightBTN_onRelease);
function RightBTN_onPress(e:MouseEvent):void
{
movingRight = 1;
}
function RightBTN_onRelease(e:MouseEvent):void
{
character.gotoAndStop(4);
movingRight = 0;
}
You can also use one function for all your event listeners like this :
stage.addEventListener(KeyboardEvent.KEY_DOWN, move_character);
stage.addEventListener(KeyboardEvent.KEY_UP, move_character);
LeftBTN.addEventListener(MouseEvent.MOUSE_DOWN, move_character);
LeftBTN.addEventListener(MouseEvent.MOUSE_UP, move_character);
RightBTN.addEventListener(MouseEvent.MOUSE_DOWN, move_character);
RightBTN.addEventListener(MouseEvent.MOUSE_UP, move_character);
function move_character(e:*){
var event_type:String = e.type; // get event type : to identify which kind of event is fired
var current_target:String = e.currentTarget.name; // get target name : to identify the target of the fired event
switch (event_type){
case MouseEvent.MOUSE_DOWN :
if(current_target == 'LeftBTN') movingLeft = 1;
else movingRight = 1;
break;
case MouseEvent.MOUSE_UP:
if(current_target == 'LeftBTN'){
character.gotoAndStop(1);
movingLeft = 0;
} else {
character.gotoAndStop(4);
movingRight = 0;
}
break;
case KeyboardEvent.KEY_DOWN:
if (e.keyCode == Keyboard.RIGHT){
movingRight = 1;
} else if (e.keyCode == Keyboard.LEFT) {
movingLeft = 1;
}
break;
case KeyboardEvent.KEY_UP:
if (e.keyCode == Keyboard.LEFT) {
character.gotoAndStop(1);
movingLeft = 0;
} else if (e.keyCode == Keyboard.RIGHT) {
character.gotoAndStop(4);
movingRight = 0;
}
break;
}
}
OR, using the e parameter as an Event :
function move_character(e:Event){
var event_type:String = e.type; // get event type : to identify which kind of event is fired
var current_target:String = e.currentTarget.name; // get target name : to identify the target of the fired event
var key_code:Number;
switch (event_type){
case MouseEvent.MOUSE_DOWN :
if(current_target == 'LeftBTN') movingLeft = 1;
else movingRight = 1;
break;
case MouseEvent.MOUSE_UP:
if(current_target == 'LeftBTN'){
character.gotoAndStop(1);
movingLeft = 0;
} else {
character.gotoAndStop(4);
movingRight = 0;
}
break;
case KeyboardEvent.KEY_DOWN:
key_code = KeyboardEvent(e).keyCode;
if (key_code == Keyboard.RIGHT){
movingRight = 1;
} else if (key_code == Keyboard.LEFT) {
movingLeft = 1;
}
break;
case KeyboardEvent.KEY_UP:
key_code = KeyboardEvent(e).keyCode;
if (key_code == Keyboard.LEFT) {
character.gotoAndStop(1);
movingLeft = 0;
} else if (key_code == Keyboard.RIGHT) {
character.gotoAndStop(4);
movingRight = 0;
}
break;
}
}
Hope that can help.
Put two buttons on the stage, name them like btnRight and btnLeft, then use this code
btnLeft.addEventListener(MouseEvent.CLICK, onButtonClick, false, 0, true);
btnRight.addEventListener(MouseEvent.CLICK, onButtonClick, false, 0, true);
function onButtonClick(evt:MouseEvent) {
if (evt.target == btnLeft) movingLeft = 1;
else movingRight = 1;
}
I would also suggest using only one var named movingDirection which can be 1, 0 or -1 :)
im trying to play a moveiclip from the library with a key press and i have as linked it but it still does not work (this is only a bit of the code there are event listeners and handlers in place and the character moves fine)
stage.addEventListener(Event.ENTER_FRAME, Move);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPress);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUp);
var aP:Boolean = false;
var dP:Boolean = false;
//creating a new Character_right instance
var character_right:Character_right = new Character_right();
function Move(vet:KeyboardEvent)
{
if(aP)
{
char.x -= 5;
char.scaleX = -0.55;
}
if(dP)
{
char.x += 5;
char.scaleX = -0.55;
}
}
function keyPress(evt:KeyboardEvent)
{
switch(evt.keyCode)
{
case Keyboard.A:
{
aP = true;
break;
}
case Keyboard.D:
{
dP = true;
character_right.play();
break;
}
}
}
function keyUp(evt:KeyboardEvent)
{
switch(evt.keyCode)
{
case Keyboard.A:
{
aP = false;
break;
}
case Keyboard.D:
{
dP = false;
break;
}
}
}
In your code, you are executing the function Character_right.play() after break and break stops the rest of the loop code from being executed, so execute Character_right.play() before break, for example:
function keyPress(evt:KeyboardEvent)
{
switch(evt.keyCode)
{
case Keyboard.A:
{
aP = true;
break;
}
case Keyboard.D:
{
dP = true;
Character_right.play();
break;
}
}
}
Check this post, with a nice tutorial about keyboard events, I believe will be important for you to understand the concept and implement on your logic.
Briefly it's something like the following example, but remember about to implement all necessary logic for your scope.
var isRight:Boolean;
var isLeft:Boolean;
var isUp:Boolean;
var isDown:Boolean;
//creating a new Character_right instance
var character_right:Character_right = new Character_right();
stage.addEventListener(KeyboardEvent.KEY_DOWN, downKeyHandler, false, 0, true);
function downKeyHandler(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.RIGHT )
{
isRight = true;
}
if (event.keyCode == Keyboard.LEFT)
{
isLeft = true;
}
if (event.keyCode == Keyboard.UP)
{
isUp = true;
}
if (event.keyCode == Keyboard.DOWN)
{
isDown = true;
}
}
stage.addEventListener(KeyboardEvent.KEY_UP, upKeyHandler, false, 0, true);
function upKeyHandler(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.RIGHT)
{
isRight = false;
}
if (event.keyCode == Keyboard.LEFT)
{
isLeft = false;
}
if (event.keyCode == Keyboard.UP)
{
isUp = false;
}
if (event.keyCode == Keyboard.DOWN)
{
isDown = false;
}
}
stage.addEventListener(Event.ENTER_FRAME, loopHandler, false, 0, true);
function loopHandler(event:Event):void
{
if (isRight)
{
//do something here…
character_right.play();
}
if (isLeft)
{
//do something here...
}
if (isUp)
{
//do something here...
}
if (isDown)
{
//do something here...
}
}