Why does Lib.current.stage give me an error? - listener

Why Lib.current.stage gives me this error:
src/Main.hx:43: characters 2-35 : openfl.display.DisplayObject cannot be called
Edit: I have tried the following code.
Lib.current.stage.addEventLitener(
KeyboardEvent.KEY_DOWN,
function(e:KeyboardEvent) {
if (e.keyCode == Keyboard.W) up = true;
if (e.keyCode == Keyboard.A) left = true;
if (e.keyCode == Keyboard.D) right = true;
if (e.keyCode == Keyboard.S) down = true;
}
);

I think MovieClip implements Dynamic<MovieClip> so that any reference not in the class definition is assumed to be a MovieClip.
You've got a typo there on addEventLitener, missing an 's' which isn't in the class definition, so is assumed to be a MovieClip, which cannot be called.

Related

Why does flash output a TypeError: Error #2007: Parameter child must be non-null. at flash.display::DisplayObjectContainer/addChild()

This is my code that I have so far
package {
import flash.display.MovieClip;
import flash.events.*;
public class Main extends MovieClip {
var leftpressed:Boolean = false;
var rightpressed:Boolean = false;
public function Main()
{
//constructor code
Spaceship.addEventListener(Event.ENTER_FRAME, moveToKey);
stage.addEventListener(KeyboardEvent.KEY_DOWN, setKeyPress);
stage.addEventListener(KeyboardEvent.KEY_UP, setKeyUnpress);
stage.addEventListener(KeyboardEvent.KEY_DOWN, FlyBullet);
}
function moveToKey (event:Event)
{
if (leftpressed && (Spaceship.x>0))
{
Spaceship.x = -5;
}
if (rightpressed && (Spaceship.x<550))
{
Spaceship.x = +5;
}
}
function setKeyPress(e:KeyboardEvent):void
{
if (e.keyCode == 37)
{
leftpressed = true;
}
if (e.keyCode == 39)
{
rightpressed = true;
}
}
function setKeyUnpress(e:KeyboardEvent): void
{
if (e.keyCode == 37)
{
leftpressed = false;
}
if (e.keyCode == 39)
{
rightpressed = false;
}
}
function FlyBullet (e:KeyboardEvent):void
{
if (e.keyCode == 32)
var bulletshot:Bullet = new Bullet();
addChild(bulletshot);
bulletshot.x = Spaceship.x;
bulletshot.y = Spaceship.y
}
}
}
With the biggest issue with the final FlyBullet function that outputs an the error listed as
TypeError: Error #2007: Parameter child must be non-null.
at flash.display::DisplayObjectContainer/addChild()
at Main/FlyBullet()
This is your problem:
if (e.keyCode == 32)
var bulletshot:Bullet = new Bullet();
addChild(bulletshot);
bulletshot.x = Spaceship.x;
bulletshot.y = Spaceship.y
There are no curly brackets so the if condition only applies to the first line where bulletshot is assigned. In other words, if you press any key other than SPACE it will skip the assignment of bulletshot but still try to addChild(bulletshot), but since bulletshot was not assigned a value it is null and you get errors.
I think you meant this:
if (e.keyCode == Keyboard.SPACE) {
var bulletshot:Bullet = new Bullet();
addChild(bulletshot);
bulletshot.x = Spaceship.x;
bulletshot.y = Spaceship.y
}
PS: Your code is poorly indented in a lot of places. It helps to have correctly indented code. An auto-formatter could help you here.
It is generally considered bad practice to omit brackets in multiline if statements, this is an example why. Whenever e.keyCode != 32 the addChild line will try to run.
Solution would be:
///...in the Main function//
stage.addEventListener(KeyboardEvent.KEY_DOWN, flyBullet);
///...
function flyBullet (e:KeyboardEvent):void
{
if (e.keyCode == 32)
{
var bulletshot:Bullet = new Bullet();
addChild(bulletshot);
bulletshot.x = Spaceship.x;
bulletshot.y = Spaceship.y;
}
}
Some unrelated as3 code quality tips, to avoid possible errors later:
I'm guessing Spaceship isn't a static class, so i suggest using lowercase first letters for variable/function names, and uppercase for classes, but this is just good practice and does not affect your codes correctness.
It is also good practice to define the accessibility of your functions, i.e public, private, internal, protected. If you omit it the default is internal.
And only skip brackets after conditionals when necessary.

Only rotate last clicked MovieClip

I have a project where a MovieClip gets copied when clicked and dragged. It is also possible to rotate and scale the MovieClip with arrow keys.
But this only rotates last MovieClip dragged, not last MovieClip clicked. So if I drag a new copy, the previous dragged copies are not able to rotate or scale.
How can I change this to only make the last MovieClip clicked able to rotate and scale, when arrow keys are pressed?
Here is some of the code used (taken and slightly modified from another post):
addEventListener(Event.ENTER_FRAME, moveObject);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyHit);
stage.addEventListener(KeyboardEvent.KEY_UP, noKeyHit);
function keyHit(e:KeyboardEvent):void{
if(e.keyCode == Keyboard.LEFT) leftArrow = true;
if(e.keyCode == Keyboard.RIGHT) rightArrow = true;
if(e.keyCode == Keyboard.UP) upArrow = true;
if(e.keyCode == Keyboard.DOWN) downArrow = true;
}
function noKeyHit(e:KeyboardEvent):void{
if(e.keyCode == Keyboard.LEFT) leftArrow = false;
if(e.keyCode == Keyboard.RIGHT) rightArrow = false;
if(e.keyCode == Keyboard.UP) upArrow = false;
if(e.keyCode == Keyboard.DOWN) downArrow = false;
}
function moveObject(e:Event):void{
if(leftArrow) latestClone.rotation -=0.75;
if(rightArrow) latestClone.rotation +=0.75;
if(upArrow) latestClone.scaleY +=0.01
if(downArrow) latestClone.scaleY -=0.01;
}
Thank you
In your code you are scaling and rotating latestClone.
Somewhere outside of this code you are setting the value of latestClone.
If you want to rotate the latest clicked MovieClip, add an event listener to the click:
latestClone.addEventListener(MouseEvent.CLICK, onCloneClick);
function onCloneClick(e:MouseEvent):void
{
var target:MovieClip = e.currentTarget as MovieClip;
latestClone = target;
}

AS3 keyboard animated character - Compiler errors all over the place despite following tutorial to the letter

Trying to create a 2D character that responds to keyboard inputs in Flash Professional CC. Every YouTube tutorial my son and I have tried produces Compiler errors and NOTHING works.
import flash.events.KeyboardEvent;
import flash.events.Event;
var rightKeyIsDown:Boolean = false;
var leftKeyIsDown:Boolean = false;
var upKeyIsDown:Boolean = false;
var downKeyIsDown:Boolean = false;
var playerSpeed:int = 7;
stage.addEventListener(KeyboardEvent.KEY_DOWN, pressAKey);
stage.addEventListener(KeyboardEvent.KEY_UP, releaseAKey);
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;
}
}
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;
}
}
player_mc.addEventListener(Event.ENTER_FRAME, moveThePlayer);
function moveThePlayer(event:Event):void
{
if(rightKeyIsDown == true)
{
player_mc.x += playerSpeed;
}
if(leftKeyIsDown == true)
{
player_mc.x -+ playerSpeed;
}
}
What is wrong with the above code? We have followed this tutorial, which seems very straightforward however the minute we type in a curly bracket it highlights in red, and then when we try to test we receive Compiler Errors.
Scene 1, Layer 'Player', Frame 1, Line 35, Column 27 1084: Syntax error: expecting rightparen before dot.
We are using Flash Professional CC and AS3 - is there a better resource for tutorials for this type of thing as we're being driven mad having tried at least 5 of these now without success.
Would really appreciate some advice on why the above is wrong and also where we might find tutorials that actually work!
Thank you.
NJ & Son! :)
I threw your code into Flash and did not see any issues surrounding mismatching parenthesis; however, I did notice the following errors
First, In the following if control structure:
if(leftKeyIsDown == true)
{
player_mc.x -+ playerSpeed;
}
You are attempting to use an operator of -+ but no such operator exists in Flash ActionScript. I'm thinking you meant to do the following instead:
if(leftKeyIsDown == true)
{
player_mc.x -= playerSpeed;
}
Second, the issue is right here:
function releaseAKey(event.KeyboardEvent):void
{
// your code
}
You need a : to specify the datatype. Can't believe I missed that. So it should be:
function releaseAKey(event:KeyboardEvent):void
{
// your code
}
I was able to plug this code into Flash CC and get it to compile with no errors. Cheers!

Firing bullets on a certain frame.

I could not find topic like this and that is why I am posting this question. I have a side scrolling game with the character set up in positions with frame labels- animation states to stand, jump, run, kneel and fire. She is also made to fire bullets with separate class file. The problem is that right now she is firing bullets in all of the animation states. The question is how do I make this character fire the bullets only on the frame label fire. The frame label fire consists of 2 frames and I want the launching of the bullet to happen on the second frame. Which means that the keyboard space will be pressed for a second or two before it goes to this frame and then it will fire (like in real life).
I tried to connect the animation state to the bullets somehow and tried to put the condition in these lines of code somehow:
if(e.keyCode == Keyboard.SPACE){
if (Animation state "Fire (2)")
fireBullet();
}
But it did not work, it doesn't know what I am talking about. The class file for the bullet is separate and I don't think is relevant to the problem.
The rest of the timeline code is like this:
var bulletList:Array = new Array();
if(e.keyCode == Keyboard.SPACE){
fireBullet();
}
}
function fireBullet():void
{
var playerDirection:String;
if(player.scaleX < 0){
playerDirection = "left";
} else if(player.scaleX > 0){
playerDirection = "right";
}
var bullet:Bullet = new Bullet(player.x - scrollX, player.y - scrollY,
playerDirection xSpeed);
back.addChild(bullet);
Thank you. I hope that my question is clear.
There is addFrameScript, AS3's mystery function that can be useful in this situation. And that would look something like this:
playerAnimation.addFrameScript( insertFrameNumber, fireBullet );
addFrameScript runs the method passed as a parameter when the MovieClip reaches a certain frame.
I believe how you would want to use this in your current code:
player.addFrameScript( 5, fireBullet );
Then in your KEY_UP handler:
function keyUpHandler(e:KeyboardEvent):void{
if(e.keyCode == Keyboard.SPACE){
spacePressed = false;
player.gotoAndPlay("fire");
}
if(e.keyCode == Keyboard.LEFT){
leftPressed = false;
}
else if(e.keyCode == Keyboard.RIGHT){
rightPressed = false;
}
else if(e.keyCode == Keyboard.UP){
upPressed = false;
}
else if(e.keyCode == Keyboard.DOWN){
downPressed = false;
}
}

Flash CS4/AS3: trying to make part of a movieclip loop only while keyboard button is pressed

I'm working on a project in which a character walks forwards or backwards across the screen or shoots a rifle depending on whether the user presses one of the following keyboard keys: forward arrow (key 39), back arrow (key 37) or spacebar (key 32).
My problem is that when the user presses and holds the forward arrow key, the character's movieclip plays one instance of the walking forward animation, and then moves forward. I want the walking forward animation to play throughout the entire time the character is moving.
Here is my code:
import fl.transitions.Tween;
import fl.transitions.easing.*;
stage.addEventListener(KeyboardEvent.KEY_DOWN, moveCharacter);
stage.addEventListener(KeyboardEvent.KEY_UP, stopCharacter);
var muzzlePosition:Number = new Number();
var bullet:Bullet = new Bullet();
function moveCharacter(e:KeyboardEvent):void {
switch (e.keyCode) {
case 39 :
if (sprite_Cicada.x<stage.stageWidth-150) {
sprite_Cicada.gotoAndPlay("walk-fwd");
sprite_Cicada.x+=5;
} else {
sprite_Cicada.x+=0;
sprite_Cicada.gotoAndPlay("push");
}
break;
case 37 :
if (sprite_Cicada.x>225) {
sprite_Cicada.x-=3;
sprite_Cicada.gotoAndPlay("walk-bkwds");
} else {
sprite_Cicada.x-=0;
sprite_Cicada.gotoAndPlay("standing");
}
break;
case 32 :
muzzlePosition=sprite_Cicada.x+sprite_Cicada.AK47.x+28;
addChild(bullet);
bullet.gotoAndStop("lead");
bullet.x=muzzlePosition;
bullet.y=328;
sprite_Cicada.gotoAndPlay("fireAK");
var shootBullet:Tween=new Tween(bullet,"x",None.easeOut,muzzlePosition,stage.stageWidth*2,.5,true);
if (bullet.x>stage.stageWidth+50) {
removeChild(bullet);
}
break;
}
}
function stopCharacter(e:KeyboardEvent):void {
sprite_Cicada.gotoAndPlay("standing");
}
Perhaps you can do
stage.addEventListener(KeyboardEvent.KEY_DOWN, keydown );
stage.addEventListener(KeyboardEvent.KEY_UP, keyup);
addEventListener(Event.ENTER_FRAME, movePerson);
function keydown(e:KeyboardEvent) {
if (e.keyCode == Keyboard.LEFT || e.keyCode == Keyboard.RIGHT) { per_mc.gotoAndStop(2) }
if (e.keyCode == Keyboard.LEFT) {leftkeyStatus = true; rightkeyStatus = false;}
if (e.keyCode == Keyboard.RIGHT) {leftkeyStatus = false; rightkeyStatus = true;}
}
function keyup(e:KeyboardEvent) {
if (e.keyCode == Keyboard.LEFT || e.keyCode == Keyboard.RIGHT) { per_mc.gotoAndStop(1) }
if (e.keyCode == (Keyboard.LEFT)) {leftkeyStatus = false;}
if (e.keyCode == (Keyboard.RIGHT)) {rightkeyStatus = false;}
}
function movePerson(e:Event) {
if (rightkeyStatus) {
sprite_Cicada.x-=3;
}
}