I'm new to Actionscript 3 and I'm wanting to allow a circle to move down using the down arrow on the keyboard. Here's my code:
package {
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class Circle extends MovieClip {
public function Circle() {
// constructor code
var speed:int = 3;
addEventListener(KeyboardEvent.KEY_DOWN,keyIsDown);
function keyIsDown(event:KeyboardEvent) {
if(event.keyCode == Keyboard.DOWN) {
y = y+=speed;
}
}
}
}
}
When I test it, nothing happens when I press the down key. Anyone know what's wrong with the code?
Try adding KeyBoard events to the stage instead of to the class. Additionally, I would not nest functions like that, bad practice in general. Also the line y = y+=speed; is confusing, shouldn't it just be y += speed; ?
EDIT: Sorry, I guess stage will be null in the constructor, I've added a ADDED event listener.
Try this:
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class Circle extends MovieClip {
public function Circle() {
// constructor code
var speed:int = 3;
addEventListener(Event.ADDED, onAdded);
}
private function onAdded(event:Event) {
stage.addEventListener(KeyboardEvent.KEY_DOWN,keyIsDown);
}
private function keyIsDown(event:KeyboardEvent) {
if(event.keyCode == Keyboard.DOWN) {
y += speed;
}
}
}
}
Related
How can I force my button to hide or show or change appearance on click?
I use actionscript3 by adobe animate.
Mouse over and mouse down are working. But on release I see mouse up image.
Note: I shouldn't have more than one scene because all of my buttons are in one scene. anyone can help? Thanks
package
{
import fl.controls.*;
import flash.display.*;
import flash.events.*;
import flash.utils.*;
import flash.ui.*;
dynamic public class Main extends MovieClip
{
public var MyButton:MovieClip;
public var HideMouseTimer:Timer;
public function Main()
{
this.MyButton.addEventListener(MouseEvent.CLICK, this.onMyButton);
this.HideMouseTimer = new Timer(5000, 1);
this.HideMouseTimer.addEventListener(TimerEvent.TIMER, this.HideMouseTimerEvent);
setInterval(this.HideMouse, 100);
}
public function HideMouseTimerEvent(event:TimerEvent) : void
{
Mouse.hide();
this.MyButton.visible = false;
}// end function
public function HideMouse() : void
{
if (this.mouseXprev == mouseX && this.mouseYprev == mouseY)
{
this.HideMouseTimer.start();
}
else
{
Mouse.show();
this.MyButton.visible = true;
this.HideMouseTimer.reset();
}
this.mouseXprev = mouseX;
this.mouseYprev = mouseY;
}// end function
public function onMyButton (event:MouseEvent) : void
{
trace ("Pressed MyButton");
}
} //end Class Main
} //end Package
I'm trying to make a ship game and I'm having an problem to get opponents fading as well.
Well, these opponents (like ships) has an class. In this class I do a interval to make its children fly to left (changing X per velocity number choosen on fourth argument in the function for adding enemies (addOpponent(opponentX, opponentY, opponentType, opponentVelocity)) and, when any of them has coordinate X smaller than -25, must be removed, through class block of itself.
package {
import flash.display.*
import flash.display.MovieClip;
import flash.utils.setTimeout;
import flash.utils.setInterval;
import flash.utils.clearInterval;
public class opponentNave extends MovieClip {
public function opponentNave(opponentVelocitySet) {
var loopMoveClassicOpponentsNave:uint = setInterval(movingClassicOpponentNave, 58);
function movingClassicOpponentNave() {
if (x < -25) {
clearInterval(loopMoveClassicOpponentsNave);
this.parent.removeChild(this);
} else {
x -= opponentVelocitySet;
}
}
}
}
}
I'm using this.parent.removeChild(this). I'm getting a error when the opponent X is smaller than -25, and it's on that time I want to remove the opponent child.
Here is how I would refactor this: (see code comments)
package
{
import flash.display.MovieClip;
import flash.events.Event;
public class opponentNave extends MovieClip
{
//create a class scoped variable for the velocity
private var velocitySet:Number;
public function opponentNave(opponentVelocitySet)
{
//set the velocity var
velocitySet = opponentVelocitySet;
//wait for this object (opponentNave) to be added to the display before doing anything display oriented
this.addEventListener(Event.ADDED_TO_STAGE, addedToStage, false, 0, true);
}
private function addedToStage(e:Event):void {
//run a function every frame tick of the application's fps
//this is best for things that are display oriented instead of time based ways like Timer or Intervals
this.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
}
private function enterFrameHandler(e:Event):void
{
if (x < -25){
if (this.parent) this.parent.removeChild(this);
this.removeEventListener(Event.ENTER_FRAME, enterFrameHandler);
}
else
{
x -= velocitySet;
}
}
}
}
I've been trying for a few hours now and I cant get my little character to move with the keyboard.
I have ran a trace to make see if anything was happening and the position value does change but my character doesn't react to that position change.
I receive no errors. Both my Character and BrickBlock are movieclips and they have been imported for ActionScript.
If any other information is needed please let me know. Thank you! :)
My following code:
package {
import flash.events.Event
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class CharMove extends MovieClip {
var char1 :Character;
var block :BrickBlock;
public function CharMove()
{
char1 = new Character();
block = new BrickBlock();
//this.addEventListener(Event.ENTER_FRAME, collide)
stage.addEventListener(KeyboardEvent.KEY_DOWN, kDown);
}
/*function collide(e:Event):void
{
if(char.hitTestObject(block))
{
char.visible = !char.visible;
}
}*/
function kDown(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.LEFT:
char1.x -= 5;
trace(char1.x);
break;
case Keyboard.RIGHT:
char1.x +=5;
trace(char1.x);
break;
}
}
}
}
You might want to consider writing a static Input class.
package input {
import flash.display.Stage;
import flash.events.KeyboardEvent;
public class Input {
private static var keys:Array = new Array(255);
public static function setup(stage:Stage):void {
stage.addEventListener(KeyboardEvent.KEY_DOWN, KeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, KeyUp);
}
private static function keyDown(e:KeyboardEvent):void {
keys[e.keyCode] = true;
}
private static function keyUp(e:KeyboardEvent):void {
keys[e.keyCode] = false;
}
public static function isKeyDown(k:int):Boolean {
return keys[k];
}
public static const A:uint = 65;
public static const B:uint = 66;
public static const C:uint = 67;
// The rest of the keys...
}
}
To use it first call setup() which adds the listeners for KEY_DOWN and KEY_UP events.
They you can easily query keys and do relevant actions accordingly.
Input.setup(stage);
/...
if(Input.isKeyDown(Input.A)) {
char1.x -= 5;
}
Main.as
package{
import flash.display.MovieClip;
import flash.events.*;
public class Main extends MovieClip {
public var _root:MovieClip;
public var monsterContainer:MovieClip = new MovieClip();
public var delay = 30;
public function Main(){
addEventListener(Event.ADDED, beginClass);
addEventListener(Event.ENTER_FRAME, enterFrameEvents);
}
function beginClass(e):void{
_root = MovieClip(root);
}
function enterFrameEvents(e):void{
addChild(monsterContainer);
delay -= 1;
if(delay <= 0){
var spawn:Slime = new Slime();
spawn.x = startPoint.x;
spawn.y = startPoint.y;
monsterContainer.addChild(spawn);
delay = 30;
}
}
}
Arrow.as
package{
import flash.display.MovieClip;
import flash.events.*;
public class Arrow extends MovieClip {
public var _root:MovieClip;
public var facingID;
public function Arrow(){
addEventListener(Event.ADDED, beginClass);
addEventListener(Event.ENTER_FRAME, enterFrameEvents);
}
function beginClass(e):void{
_root = MovieClip(root);
}
function enterFrameEvents(e):void{
trace(_root.monsterContainer == null);
}
}
Enemy.as
package{
import flash.display.MovieClip;
import flash.events.*;
public class Enemy extends MovieClip {
public var _root:MovieClip;
//Status
public var monsterSpeed;
public var facing = "Right";
//CallingArrow
public var down:Down = new Down();
public function Enemy(){
addEventListener(Event.ADDED, beginClass);
addEventListener(Event.ENTER_FRAME, enterFrameEvents);
}
function beginClass(e):void{
_root = MovieClip(root);
}
function enterFrameEvents(e):void{
//Facing Movement
if(_root.pausing == false){
if(facing == "Right"){
this.x += monsterSpeed;
}else if(facing == "Left"){
this.x -= monsterSpeed;
}else if(facing == "Down"){
this.y += monsterSpeed;
}else if(facing == "Up"){
this.y -= monsterSpeed;
}
}
}
}
Down.as
package {
import flash.display.MovieClip;
import flash.events.*;
public class Down extends Arrow {
public function Down(){
facingID = "Down";
}
}
Slime.as
package {
import flash.display.MovieClip;
import flash.events.*;
public class Slime extends Enemy {
public function Slime(){
monsterSpeed = 5;
}
}
and there is no additional code on timeline just stop();
I got 1119 error, when i want to access a movieClip inside slime, i give it monster for the instance name, please help !
Download Link : http://www.mediafire.com/download/hz5tptkgftwdipw/Tower_Defense.rar
It's only 15KB and using CS6 Please help !
Turn on Debugging
The code you're sharing is more than you probably need (.rar file included). To find the cause of the problem you (and those on StackOverflow) need to know what line you're programming is running into this error. If you're using Flash IDE CS6, the can be enabled by going to your publish settings and enabling "Permit Debugging". This will take your ambiguous error...
null object reference at myDocument/doSomething()
...to a much clearer...
null object reference at myDocument/doSomething() package\myClass.as:20
...which now denotes which line in your code to look for your issue.
Use the Debug Console
Use the debugging compile mode to bring up the Debug Console. This will provide you with an immediate look at the line of code in question, as well as the Call Stack, and the state of all available Variables. No programmer should be without it.
Enemy.monster
This is the crux of the issue: somewhere, you're calling on Enemy.monster, and there is no property on your Enemy class that's called that (method or otherwise).
Ives tried many hit detections and none of them seem to work for me. I've tried hittest hittestobject hitarea. When my object (which is a or b movie-clip goes fully into c movie clip i want c to move 300 x direction. Does not need to be pin point detection just as long as its in the c movie-clip it works.
package {
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.media.Sound;
import flash.ui.Mouse;
public class bakingCake extends MovieClip {
public function bakingCake() {
// constructor code
var object:MovieClip = new MovieClip;
a.addEventListener(MouseEvent.MOUSE_DOWN,objectA);
b.addEventListener(MouseEvent.MOUSE_DOWN,objectB);
if (object.hitArea(c) == true)
{
c.x = 300;
}
function objectA():void
{
object = a;
object.addEventListener(MouseEvent.MOUSE_OVER,objectFun);
}
function objectB():void
{
object = b;
object.addEventListener(MouseEvent.MOUSE_OVER,objectFun);
}
function objectFun(event:MouseEvent):void
{
object.addEventListener(MouseEvent.MOUSE_DOWN,drag);
object.addEventListener(MouseEvent.MOUSE_UP,sDrag);
}
function drag(event:MouseEvent):void
{
object.startDrag();
}
function sDrag(event:MouseEvent):void
{
object.stopDrag();
}
}
}
}
Did you tried getBounds() ?
I suggest condition:
if (c.getBounds(c.parent).containsRect(a.getBounds(c.parent))
|| c.getBounds(c.parent).containsRect(b.getBounds(c.parent))) {
c.x = 300;
}
IMO the best way is check it triggered by ENTER_FRAME event, atached for any of object.