Flash as3 stop(); not working? - actionscript-3

I put my code in a out side as3 file and when i try to use the trace it works fine, but when a i add a stop after, it won't work, why?
here is my code:
package {
import flash.display.Sprite;
public class TDSBMaze extends Sprite {
public function TDSBMaze() {
trace("Test");
stop();
}
}
}
And also when i try to just put the code in a frame it also won't work unless i unlink the outside .as file.

Sprites do not have a timeline, so it can't stop since it never plays. If you need a timeline, you should extend MovieClip.

You need to extend MovieClip if you wish to make use of a timeline. Sprites do not have timelines.
package {
import flash.display.MovieClip;
public class TDSBMaze extends MovieClip {
public function TDSBMaze() {
trace("Test");
stop();
}
}
}

Related

Can't click on a class in flash, can't add click EventListener in class .as file :C

Alright so I have laboured all day to make an Enemy appear in my flash game, one that will go away after a certain number of clicks, however I learned that I can't click on EnemyShip in the main file, so I have to add the click listener into the .as package file. When I attempt to do this, I get the error 5006: An Actionscript file cannot have more than one externally visible definition: clickCount, EnemyShip.
Research suggests this is some sort of curly brace formatting error but I honestly have no clue what to do. :c Please help! And please use simple words, I am a noob with Actionscript 3 ;0;
package{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent
var clickCount:int = 0;
public class EnemyShip extends MovieClip{
public function EnemyShip(){
this.x = 900;
this.y = 214;
addEventListener("enterFrame", enterFrame);
addEventListener(MouseEvent.CLICK, addClick);
}
function addClick(event:MouseEvent):void {
clickCount++;
trace ("clickage");
}
function enterFrame(e:Event):void{
if(this.x < -100){
removeEventListener("enterFrame", enterFrame);
stage.removeChild(this);
}
}
}
}
Your issue is likely this line:
var clickCount:int = 0;
It is not contained within your class and is with the imports. All code needs to be wrapped in the class declaration.
If you move that line so it's within your class, the error should go away.
package{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent
public class EnemyShip extends MovieClip {
private var clickCount:int = 0;
....rest of code
nevermind, i figured out what happened. the hit clip was not on the main stage, it was contained in a movie clip. i moved the actionscript and the hit clip onto the main stage and received a trace.

How do I refer to MovieClips / variables of the main .fla file from inside a MovieClip class?

package
{
import flash.display.MovieClip;
import flash.events.Event;
public class OtherMc extends MovieClip
{
public function OtherMc()
{
addEventListener(Event.ENTER_FRAME, eframe);
}
private function eframe(event:Event):void
{
if (hitTestObject(MainPacI))
{
trace("All Good");
}
}
}
}
I have a MovieClip instance on the stage of the mail .fla file called MainPacI and when I run the program, I get the error - 1120:Access of undefined property MainPacI. I apologize if the question is stupid, but I really need to know.
Regards,
Dust
Try using the following
stage.MainPacI

AS3 - Can Bitmap classes dispatch mouse events?

I'm trying to learn AS3 and have run into a small problem.
I have a Bitmap class to which I add a MouseEvent.CLICK listener, but the event doesn't seem to be dispatched.
I use Flashdevelop to write AS3 code and Flex to compile.
I have two classes, Enemy.as and Player.as
The Player.as looks like this:
package Player
{
import flash.display.Sprite;
import flash.events.MouseEvent;
[Embed(source="../../assets/leek.swf", symbol="Leek")]
public class Player extends Sprite
{
public function Player()
{
trace("Player constructed");
addEventListener(MouseEvent.CLICK, handleClick);
}
private function handleClick(e:MouseEvent):void
{
trace("Clicked Player");
}
}
}
The Enemy.as looks like this:
package enemies
{
import flash.display.Bitmap;
import flash.events.MouseEvent
[Embed(source="../../assets/gardengnome.png")]
public class Enemy extends Bitmap
{
public function Enemy()
{
trace("enemy constructed");
addEventListener(MouseEvent.CLICK, handleClick);
}
private function handleClick(e:MouseEvent):void
{
trace("Clicked Enemy");
}
}
}
The two classes are pretty much identical except that one is a Sprite and I embedded a symbol from a swf file that I got from a tutorial, and the other is a Bitmap and I embedd a png file into that.
The Player class (the one that's a sprite and uses a symbol) fires off the MouseEvent.CLICK when I run the project and click on the Player image, but the Enemy class does not.
There are no compile warnings or errors, so I'm having a hard time understanding what is the issue exactly. Is it because one is a Sprite and the other a Bitmap, or is it because one uses a prepared symbol from a swf, while the other is just a png?
How can I make a Bitmap class respond to MouseEvent?
Thanks for any help!
From ActionScript® 3.0 Reference for the Adobe® Flash® Platform:
The Bitmap class is not a subclass of the InteractiveObject class, so
it cannot dispatch mouse events. However, you can use the
addEventListener() method of the display object container that
contains the Bitmap object.
Unfortunately Bitmap class doesn't dispatch mouse events you will have to wrap it inside a Sprite class.

Why can't I get access to an external class in actionscript 3?

I am working on a breakout game in adobe flash. I defined a document class, BreakOut.as, and set it to .fla file. I wrote another class Player.as, but I failed to get access to Player.as in my BreakOut.as. Here is the code:
BreakOut.as:
package
{
import flash.display.MovieClip;
import flash.display.Sprite;
public class BreakOut extends MovieClip
{
public function BreakOut()
{
var background:Background;
background= new Background();
addChild(background);
var playerone:Player;
playerone=new Player();
playerone.x=50;
playerone.y=50;
addChild(playerone);
}
}
}
Player.as:
package
{
import flash.display.MovieClip;
public class Player extends MovieClip
{
public function Player()
{
player.graphics.beginFill(0x000000);
player.graphics.drawRect(0,0,20,100);
}
}
}
Adobe flash keeps telling me: Access of undefined property Player. Well, Background.as is another class, and I can get access to it without any problems. But it just won't work on Player.as.
player.graphics.beginFill(0x000000);
player.graphics.drawRect(0,0,20,100);
By this, if you are trying to initialize Player by drawing a rectangle, you should be rather using this:
this.graphics.beginFill(0x000000);
this.graphics.drawRect(0,0,20,100);
Do note that Player.as should also be in the same path as the fla's classpath.

Cannot access ActionScript 3 Sprite.graphics.x methods

I have this code:
package graphics {
import flash.display.Sprite;
import flash.events.*;
public class Ball extends Sprite {
public function Ball(_stage){
_stage.addChild(this);
drawBall();
}
private function drawBall(){
graphics.beginFill(0x0000CC);
graphics.lineStyle(2,0x000000,1);
graphics.drawCircle(0,0,10);
graphics.endFill();
}
}
}
ADDED:
and the class that I pass to mxmlc:
package {
import flash.display.Sprite;
import graphics.*;
[SWF(width='1024', height='768', backgroundColor='#FFFFFF', frameRate='30')]
public class Application extends Sprite {
public function Application(){
var ball:Ball = new Ball(this);
}
}
}
Except that when I compile, I get the following error:
ball.as(11): col: 14 Error: Call to a possibly undefined method beginFill.
graphics.beginFill(0x0000CC);
^
Along with the other three graphics.x() calls.
I am probably doing something wrong here, but I do not know what. Do you?
Just use this.graphics.method to avoid confusions created by your package name.
Edit - after comment.
After some messing around, it seems that though this.graphics traces as a correct Graphics object as expected, the this.graphics.method is looked in the Ball class. Don't know what messes this up like this, but it can be solved with a simple casting.
package graphics {
import flash.display.Graphics;
import flash.display.Sprite;
import flash.events.*;
public class Ball extends Sprite {
public function Ball(_stage) {
_stage.addChild(this);
drawBall();
}
private function drawBall() {
var g:Graphics = this.graphics as Graphics;
g.beginFill(0x0000CC);
g.lineStyle(2,0x000000,1);
g.drawCircle(0,0,10);
g.endFill();
}
}
}
Good luck
I am afraid the problem must be elsewhere in your code. If I take your class and clean it up so it compiles like so:
package
{
import flash.display.Sprite;
public class Ball extends Sprite
{
public function Ball()
{
drawBall();
}
private function drawBall():void
{
graphics.beginFill(0x0000CC);
graphics.lineStyle(2,0x000000,1);
graphics.drawCircle(0,0,10);
graphics.endFill();
}
}
}
$ mxmlc Ball.as
Loading configuration file [...]/flex-config.xml
/private/tmp/actionscript/Ball.swf (666 bytes)
It draws a blue ball in the top left corner of the screen. So your problem with graphics must be related to code which you have not shown here.
Edit: Based on the new information:
Your namespace graphics for the class Ball conflicts with the property name graphics. You need to rename the package and the directory it lives in.
Your class can not add itself to the stage. It has no reference to the stage until it has been added to the display list. Your application class needs to both create the ball and add it to the stage.
Also, it's just stage, not _stage.