how to work with button in class and packages at AS3 - actionscript-3

I am a beginner of action script. working with button in action script class file not working.
i have created two file one is stream.as and another is main.as
main.as is the main class file of my frame.
i have drawed a button and converted it in button and gave instance name play_btn.
but the compiler giving me 1120: access of undefined property play_btn.
the both codes are given below;
main.as
package {
import flash.display.MovieClip;
import stream.stream;
public class main extends stream {
public function main() {
}
// constructor code
}
}
stream.as
package {
import flash.events.MouseEvent;
import flash.display.MovieClip;
public class stream extends MovieClip {
public function main() {
play_btn.addEventListener(MouseEvent.CLICK, pausevedio);
function pausevedio(event:MouseEvent):void{
play_btn.visible=false;
}
// constructor code
}
}
}

Correct me if I'm wrong but it's because the play_btn belongs solely to your main class and you're trying to access it through the stream class, to do this properly try to instantiate it through code in the class you wish to use it in instead of on the timeline like so:
playBtn: play_btn = new play_btn();
playBtn.x = x;
playBtn.y = y;
addChild(playBtn);
This is my best guess I'm not exactly sure how your classes are structured but this might be your issue. I hope this helps (I'm new too but I figured my two-cents might help!)!
~Cheers!

Related

AS3 addEventListener not a recognized method in my custom class

I want to have a Class called Commands that I can put different key presses and functions into that will control game states and variables for quick and easy game testing. I'm trying this, but it's not working...
package gameTesting {
import flash.events.KeyboardEvent;
import flash.events.*;
import flash.ui.Keyboard;
import flash.display.*;
import flash.events.EventDispatcher;
public class Commands {
public function Commands() {
addEventListeners();
}
public function addEventListeners():void{
addEventListener(KeyboardEvent.KEY_DOWN,keyDown);
}
public function keyDown(ke:KeyboardEvent):void{
trace("key pressed");
}
}
}
which throws this error:
C:...\Commands.as, Line 15, Column 4 1180: Call to a possibly undefined method addEventListener.
So, I tried having my class extend something that inherits the EventDispatcher methods:
//...
public class Commands extends DisplayObject{
// ...
but I just get this error thrown from my main .as file when trying to instantiate this Class:
ArgumentError: Error #2012: Commands$ class cannot be instantiated.
I also tried throwing the static keyword around just for lols, but no dice.
What am I missing here?
by the way, my reason for doing things this way is just so that I can remove this functionality (so users can't use it) by simply removing the line of code that instantiates this class. I think it's pretty nifty, but if this seems asinine, by all means speak up!
Try to pass stage to Commands,so you can add addEventListener on stage.
import flash.display.Stage;
public class Commands {
public function Commands(stage:Stage) {
stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDown);
}
public function keyDown(ke:KeyboardEvent):void{
trace("key pressed");
}
}

actionscript 3 - error #1009 issues calling function from another class

Im having trouble calling functions from other classes. I want to call a function in one class which updates a score display in another class. The error code for this is:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at code.functions::EnemyYellow()[code\functions\EnemyYellow.as:18]
at code::Main()[\code\Main.as:27]
Would appreciate if someone could help me out, I set up 2 basic files with the code which is causing an issue. Its normally not set up like this, I just made this for testing and so I can clearly explain the problem.
Main file:
package code {
import flash.display.MovieClip;
import flash.events.*;
import code.*;
public class Main extends MovieClip {
public var _enemy:EnemyYellow;
public var playerHP:Number;
public function Main() {
playerHP = 10;
_playerHPdisplay.text = playerHP.toString();
trace(playerHP)
_enemy = new EnemyYellow;
}
public function lowerHP ():void
{
playerHP = playerHP - 1;
_playerHPdisplay.text = playerHP.toString();
trace(playerHP)
}
}
}
Second File:
package code.functions {
import flash.display.MovieClip;
import flash.events.*;
import code.Main;
public class EnemyYellow extends MovieClip {
public var _main:Main;
public function EnemyYellow() {
_main.lowerHP();
trace ("done")
}
}
}
I also tried adding _main = new Main; in the second file but the game just loads with a blackscreen and an error about invalid data.
First of all, you surely need to "instantiate" the Main class, which means basically to create it.
public var _main:Main;
This line just declares that there will be a variable of type Main. But for now, the value of _main is null. So you are right, that you need to call:
_main = new Main();
After you've done this, the first error will disappear. But then you have things in that MovieClip that are still vague. Like _playerHPdisplay. Where's that from? Is it an instance from stage or what? You just created a brand new object, and it does not have any reference to other objects, TextFields or whatever.
So this basically answers your current question and problem, but for sure you will have more :)

How to access class functions during events from currentTarget object in AS3

I have loaded movieClip to stage and was performing some events on that movieClip. Movie clip has own public functions and variables, and those are NOT accessible through currentTarget object in events.
Here is sample class:
package {
import flash.display.MovieClip;
import flash.display.Shape;
public class SampleClass extends MovieClip {
var str:String;
public function SampleClass() {
str="Some string";
/* draw just a sample rectangle to click on it */
var rectangle:Shape=new Shape ;
rectangle.graphics.beginFill(0x000000);
rectangle.graphics.drawRect(0,0,100,100);
rectangle.graphics.endFill();
}
public function getStr():String {
return str;
}
}
}
And here is loading on the stage and creating event:
package {
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class MainClass extends MovieClip {
var a:SampleClass;
public function MainClass() {
a=new SampleClass();
addChild(a);
a.addEventListener(MouseEvent.CLICK,clickEvent);
}
function clickEvent(evt:MouseEvent):void {
var Obj=evt.currentTarget;
trace (Obj.getStr());
}
}
}
Tracing will return null instead of string value cause currentTarget is an Object, not a Class (movieClip). Any idea how to solve this?
//use this code it will work
function clickEvent(evt:MouseEvent):void {
var Obj:SampleClass = evt.currentTarget as SampleClass;
trace (Obj.getStr());
}
I dont know if your problem is solved now but the code you posted in your question worked okay for me..
What I did to test it..
In a new blank document, open Library (ctrl+L) and right-clicked to make symbol (MovieClip)
In linkage section, tick Export for Actionscript and call it SampleClass
Right click symbol item now added in Library list and choose Edit Class option
In that SampleClass I replace all with a paste of your code BUT NOTE: after that line rectangle.graphics.endFill();.. I also added the line addChild(rectangle);
Now when I test (Debug: Ctrl+Shift+Enter).. I see a black square that traces "Some string" everytime I click it..
Your MainClass.as was attached to the FLA as the Document Class (see Properties with Ctrl+F3)
I hope this is useful to you or anyone else trying this kind of code. Any issues just add a comment. Thanks.

How to import class from user actionscript file

I just want to create a class with two Numbers and a Bitmap. You can't have nested classes in a Timeline script so I figured I'd make .as file, namely "node.as". But I cannot for the life of me figure out how to import this class into the Timeline script so that I can use the class in my Timeline script.
Please help!
You should be able to create a package to accomplish this. The basic format is:
// folder specifies the relative folder of the package
package myfolder {
// whatever other classes you need
import flash.display.*;
import flash.events.*;
// base this on whatever class you need (in this case MovieClip)
public class myclass extends MovieClip {
private var myvariable: String;
// constructor
public function myclass() {
}
// functions only for use within the class
private function myprivatefunction() {
}
// functions for use by the rest of the world
public function mypublicfunction() {
}
}
}
The constructor is called when you create a new instance of the Class-- this is where you would do your initialization. Then, in your timeline, just add something like:
import myclass;
var myclassinstance = new myclass();
myclassinstance.mypublicfunction(); // call a function in the class
See adobe's help for more details.
So I tried this:
package {
import flash.display.*;
public class Node {
public var fX:Number;
public var fY:Number;
public var bmp:Bitmap;
public function Node() { }
}
}
And put it in a file called Node.as (note I needed to add the flash.display import, and the capital "N" in the filename). In a FLA file in the same directory, I added this to the first frame script:
import Node;
var node = new Node();
trace("node="+node);
It compiled and ran without error. There must be something else going on? What version of Flash and of ActionScript are you using?

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.