AS 3 : Existing Function doesn't found? - actionscript-3

There a strange bug on my program compilation...
For my program scenario, it's just a MovieClip names "perso" who move with keyboard's arrows.
Flash says me that "clavierUp" and "animation" property access can't be find.
I really don't understand...
var perso:Perso = new Perso();
stage.addEventListener(KeyboardEvent.KEY_DOWN, clavierDown);
stage.addEventListener(KeyboardEvent.KEY_UP, clavierUp);
stage.addEventListener(Event.ENTER_FRAME, animation);
function clavierDown(e)
{
switch(e.keyCode)
{
case Keyboard.LEFT:
perso.speedX = -speedHero;
break;
case Keyboard.RIGHT:
perso.speedX = speedHero;
break;
case Keyboard.UP:
perso.speedY = -speedHero;
break;
case Keyboard.DOWN:
perso.speedY = speedHero;
break;
}
function clavierUp(e)
{
switch(e.keyCode)
{
case Keyboard.LEFT:
perso.speedX = 0;
perso.scaleX = -1;
break;
case Keyboard.RIGHT:
perso.speedX = 0;
perso.scaleX = 1;
break;
case Keyboard.UP:
perso.speedY = 0;
break;
case Keyboard.DOWN:
perso.speedY = 0;
break;
}
}
function animation(e)
{
animeHero();
}
Thank you !

Sorry, i just missed a rightbrace at the end of "clavierDown" function...

Related

Moving running cycle in AS3

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

Arrow keys not detected once the SWF file is published

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

AS3 game development: attack movements not following suit of movement direction

I'm making a top-down RPG style game, very much a throwback to zelda. This is my code for the movement controls based on keyboard controls. The movement itself is pretty solid, no stutter-key syndrome, all animations are triggered correctly.
However, when the attack button (SPACE in this case) is pressed it triggers the appropriate frame but when the key is released the corresponding frame is still visible, until another direction key is pressed. So, when you attack it looks like he keeps his sword extended until another movement is made, and so on. This should only happen when the key is held down not when simply pressed. This is not the expected result. The result I'm looking for is an attack that triggers every time the key is pressed, creating the normal buttondown=attacking, buttonup = not attacking.
The attacks and movement now are based on a variable moving:int = 4; That way I can switch the attack position to the corresponding movement direction.
Any ideas on how to correct this issue?
import flash.events.Event;
import flash.events.KeyboardEvent;
character.stop();
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPress);// when key is pressed
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyRelease);//when key is released
stage.addEventListener(Event.ENTER_FRAME, MainLoop);
var moving:int = 4;
var movingDown:Boolean = false;
var movingUp:Boolean = false;
var movingRight:Boolean = false;
var movingLeft:Boolean = false;
function onKeyPress(e:KeyboardEvent):void
{
switch (e.keyCode)
{
case Keyboard.DOWN : movingDown = true; moving = 1; break;
case Keyboard.UP : movingUp = true; moving = 2; break;
case Keyboard.RIGHT : movingRight = true; moving = 3; break;
case Keyboard.LEFT : movingLeft = true; moving = 4; break;
case Keyboard.SPACE : handleAttack(); break;
}
}
function onKeyRelease(e:KeyboardEvent):void
{
switch (e.keyCode)
{
case Keyboard.DOWN: character.gotoAndStop(1); movingDown=false; moving = 1; break;
case Keyboard.UP: character.gotoAndStop(3);movingUp=false; moving = 2; break;
case Keyboard.LEFT: character.gotoAndStop(5);movingLeft=false; moving = 3; break;
case Keyboard.RIGHT: character.gotoAndStop(7);movingRight=false; moving = 4; break;
case Keyboard.SPACE: handleAttack(); break;
}
}
function handleAttack():void
{
switch (moving)
{
case 1: character.gotoAndStop(9); movingDown = false; break; //down
case 2: character.gotoAndStop(10); movingUp = false; break; //up
case 3: character.gotoAndStop(11); movingLeft = false; break; //left attacks
case 4: character.gotoAndStop(12); movingRight = false; break; //right
}
}
function MainLoop(e:Event):void
{
switch (true)
{
case movingDown: character.gotoAndStop(2); character.y+=4.5; break;
case movingUp: character.gotoAndStop(4); character.y-=4.5; break;
case movingLeft: character.gotoAndStop(6); character.x-=4.5; break;
case movingRight: character.gotoAndStop(8); character.x+=4.5; break;
}
}
I am kinda new myself but i think it's because both onKeyPress() and onKeyRelease() point to the same function handleAttack() and as such point to the same frame even when you release. Try making two seperate attack functions for each one.
e.g
attackPressed() for onKeyPress()
function attackPressed():void
{
switch (moving)
{
case 1: character.gotoAndStop(9); movingDown = false; break;
case 2: character.gotoAndStop(10); movingUp = false; break;
case 3: character.gotoAndStop(11); movingLeft = false; break;
case 4: character.gotoAndStop(12); movingRight = false; break;
}
}
and attackRelease() for onKeyRelease()
function attackRelease():void
{
switch (moving)
{
case 1:character.gotoAndStop(1); movingDown = false; break;
case 2:character.gotoAndStop(3); movingUp = false; break;
case 3: character.gotoAndStop(5); movingLeft = false; break;
case 4: character.gotoAndStop(7); movingRight = false; break;
}
}
Hope this helps.

How create border in stage

I need advice, please. I'm working on one project - a simple game. It will be something like "Space Invaders". I just needed to cater to the ship could not leave the area (Stage). Function, is called "RMimoXY" does not work. Could someone please check out what I'm missing in the program?
Thanks in advance for your advice.
import flash.events.KeyboardEvent;
import flash.display.Sprite;
import flash.display.DisplayObject;
import flash.media.Sound;
import flash.display.Stage;
var let: Boolean = false;
var pozadi: Stage;
var vx:Number = 0;
var vy:Number = 0;
function mezernik(){
var mySound: Sound = new laserFire();
mySound.play();
RMimoXY();
}
stage.addEventListener(KeyboardEvent.KEY_DOWN, klavesnice);
function klavesnice(e: KeyboardEvent){
switch(e.keyCode){
case Keyboard.LEFT: lod.x += -5; break;
case Keyboard.RIGHT: lod.x += 5; break;
case Keyboard.UP: lod.y += -5; break;
case Keyboard.DOWN: lod.y += 5; break;
case Keyboard.SPACE: mezernik(); break;
}
}
function RMimoXY(){
if (lod.x > stage.stageWidth ){
lod.x = 0 - lod.width;
}
else if (lod.x < 0 - lod.width ){
lod.x = stage.stageWidth;
}
if (lod.y > stage.stageHeight ){
lod.y = 0 - lod.height;
}
else if (lod.y < 0 - lod.height ){
lod.y = stage.stageHeight;
}
}
It appears as if you're only calling RMimoXY in your constructor. You should call it every time the ship is moved. So adding it to the end of your keyhandler should work:
function klavesnice(e: KeyboardEvent){
switch(e.keyCode){
case Keyboard.LEFT: lod.x += -5; break;
case Keyboard.RIGHT: lod.x += 5; break;
case Keyboard.UP: lod.y += -5; break;
case Keyboard.DOWN: lod.y += 5; break;
case Keyboard.SPACE: mezernik(); break;
}
RMimoXY();
}

ActionScript: How come i cant draw diagonally?

So, I'm learning ActionScript on my own with a book ('Foundation AS3.0 with Flash...').
And I'm stuck on an excercise.
I have to make a drawing app with using the arrowkeys and spacebar.
It doesn't work 100%. I can draw up, down, right, left. Even diagonal, only right-up, and left-down.
Do you guys know where my mistake is? I also want to draw left-up and right-down....
Here's my code so far:
package classes{
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.events.Event;
public class KeyboardDrawing extends Sprite{
private const PIXEL_DISTANCE_TO_DRAW:uint = 2;
private var _canvas:Sprite;
private var _crosshair:Shape;
private var _xDirection:int = 0;
private var _yDirection:int = 0;
private var _isDrawing:Boolean = false;
public function KeyboardDrawing(){
_canvas=new Sprite();
addChild(_canvas);
_crosshair=new Shape();
drawCrosshair();
addChild(_crosshair);
_canvas.graphics.lineStyle(2, 0x000000);
stage.focus=_canvas;
_canvas.addEventListener(KeyboardEvent.KEY_DOWN, onCanvasKeyDown);
_canvas.addEventListener(KeyboardEvent.KEY_UP, onCanvasKeyUp);
_canvas.addEventListener(Event.ENTER_FRAME, onCanvasEnterFrame);
}
private function drawCrosshair():void{
_crosshair.graphics.lineStyle(1, 0x000000);
_crosshair.graphics.moveTo(-5, 0);
_crosshair.graphics.lineTo(6, 0);
_crosshair.graphics.moveTo(0, -5);
_crosshair.graphics.lineTo(0, 6);
}
private function onCanvasKeyDown(event:KeyboardEvent):void{
switch(event.keyCode){
case Keyboard.UP:
_yDirection = -PIXEL_DISTANCE_TO_DRAW;
break;
case Keyboard.DOWN:
_yDirection = PIXEL_DISTANCE_TO_DRAW;
break;
case Keyboard.LEFT:
_xDirection = -PIXEL_DISTANCE_TO_DRAW;
break;
case Keyboard.RIGHT:
_xDirection = PIXEL_DISTANCE_TO_DRAW;
break;
case Keyboard.SPACE:
_isDrawing = true;
break;
}
}
private function onCanvasKeyUp(event:KeyboardEvent):void{
switch(event.keyCode){
case Keyboard.UP:
case Keyboard.DOWN:
_yDirection = 0;
break;
case Keyboard.LEFT:
case Keyboard.RIGHT:
_xDirection = 0;
break;
case Keyboard.SPACE:
_isDrawing = false;
break;
}
}
private function onCanvasEnterFrame(event:Event):void{
_crosshair.x += _xDirection;
_crosshair.y += _yDirection;
if (_isDrawing){
_canvas.graphics.lineTo(_crosshair.x, _crosshair.y);
}else{
_canvas.graphics.moveTo(_crosshair.x, _crosshair.y);
}
}
}
}
It's an issue with the keyboard and certain combinations of keys.
If you change the keys to WASD like so it works fine:
private function onCanvasKeyDown(event:KeyboardEvent):void{
switch(event.keyCode){
case Keyboard.W:
_yDirection = -PIXEL_DISTANCE_TO_DRAW;
break;
case Keyboard.S:
_yDirection = PIXEL_DISTANCE_TO_DRAW;
break;
case Keyboard.A:
_xDirection = -PIXEL_DISTANCE_TO_DRAW;
break;
case Keyboard.D:
_xDirection = PIXEL_DISTANCE_TO_DRAW;
break;
case Keyboard.SPACE:
_isDrawing = true;
break;
}
}
private function onCanvasKeyUp(event:KeyboardEvent):void{
switch(event.keyCode){
case Keyboard.W:
case Keyboard.S:
_yDirection = 0;
break;
case Keyboard.A:
case Keyboard.D:
_xDirection = 0;
break;
case Keyboard.SPACE:
_isDrawing = false;
break;
}
}
Basically it's due to flawed keyboard design that simplifies the wiring at the expense of some key combinations.
I found this article that explains it in full: http://www.microsoft.com/appliedsciences/antighostingexplained.mspx