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.
Related
I have a weird problem in a game which I want to create. At first I have created a project without external classes.
On the root I have three Characters and one Level. Also there is a script for the key listeners and I have eventListeners to register the level, levelElements, coins and the characters. Then I have a CharacterControl MovieClip in the library. This MovieClip contains the character behaviour. As example walk, jump, idle, gravity if not colliding to the ground. There are also different events and eventListeners.
The scripts are on the timeline. If I call in both timelines a trace-function, the root was called before the CharacterController.
After that in my next exercise I created a document class Main. Now there are all root scripts. And for the CharacterController I also copied the timeline code and put it into an external class.
Now my problem is that the CharacterController class is called before the main class gets called. This leads to the problem that the eventListener and events can't get called in right order. There are happening a few errors. No Coin and no Character collides on the ground or a plattform. Everything is falling down.
How can I achieve that the Main gets called at first? Should I remove the characters and create them by script?
EDIT:
Ok, I give a short example which shows the basic problem without the complex code of my game.
package {
import flash.display.MovieClip;
public class Main extends MovieClip {
public function Main() {
trace("main was called");
}
}
}
package {
import flash.display.MovieClip;
public class My_Circle extends MovieClip {
public function My_Circle() {
// constructor code
trace("circle was called");
}
}
}
Here are some pictures of the configuration and structure of my project:
I need Main called as first. I think it's a basic problem in as3.
You'd make the class file of your stage Main.as in the properties pane.
Edit: Interesting. Just replicated this. I believe then that flash/air constructs elements so they're ready to be put on the stage instead of constructing the stage first and elements after. You should put the code you want to execute for your circle in some sort of init function and execute it in.
package
{
import flash.display.MovieClip;
public class Main extends MovieClip
{
public function Main()
{
super();
trace("Hello");
(circle_mc as Circle).init();
}
}
}
Circle:
package
{
import flash.display.MovieClip;
public class Circle extends MovieClip
{
public function Circle()
{
super();
}
public function init():void
{
trace("World");
}
}
}
ok, I figured out a simple solution how you can make it by code. At first I think it's a better solution to create the object (circle, character, whatever) by code.
The timeline code:
import flash.events.Event;
Main.setStageRef(this.stage); //Super-important
stop();
The Main class code:
package {
import flash.display.MovieClip;
import flash.display.Stage;
import flash.display.DisplayObject;
import flash.events.Event;
public class Main extends MovieClip {
public static var stageRef : Object = null;
var movieClipExample : My_Circle;
public function Main() {
this.addEventListener(Event.ENTER_FRAME,this.afterMainTimeLine);
stage.addEventListener("myEvent", myEventFunction);
}
public function afterMainTimeLine(e:Event) {
trace("Hello");
movieClipExample = new My_Circle;
movieClipExample.init();
stageRef.addChild(movieClipExample);
removeEventListener(Event.ENTER_FRAME, this.afterMainTimeLine);
this.addEventListener(Event.ENTER_FRAME,this.updateFunction);
}
public function updateFunction(e:Event){
movieClipExample.moveFunction();
}
public function myEventFunction(_event: Event) {
trace("myEvent was triggered");
}
//Getter/setter for stage
public static function setStageRef(_stage : Object) : void
{
stageRef = _stage;
}
public static function getStageRef() : Object
{
return stageRef;
}
}
}
The Object code (as example My_Circle):
package {
import flash.display.MovieClip;
import flash.display.Stage;
import flash.display.DisplayObject;
public class My_Circle extends MovieClip {
public function My_Circle()
{
stageRef = Main.getStageRef();
trace("World");
this.x = x;
this.y = y;
var evt = new Event("myEvent", true);
stageRef.dispatchEvent(evt);
}
public function init():void
{
trace("Created Circle");
this.x = 300;
this.y = 100;
}
function moveFunction(){
this.y += 1;
}
}
}
The output is following:
Hello
World
myEvent was triggered
Created Circle
Maybe this could be helpful for others.
Just one thing. For different objects from the same class it's better to use an array. The position (maybe) should be random.
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");
}
}
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!
I'm new to AS3 and seem to be overlooking something related to classes and/or targeting. I have a MovieClip in my Library that is linked to a class called 'cloud' (yes cloud is just a picture of a rain cloud). I want to add cloud to the stage. I understand there are two ways to do this:
Drag it directly across from the Library to the Stage.
Use AS3 to addChild to the Stage.
The class works when I publish my swf using the "drag it directly across from the Library to the Stage" approach. But it doesn't work when I try using the AS3 approach.
The class is as follows:
package {
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
public class cloud extends MovieClip {
public function cloud() {
stage.addEventListener(KeyboardEvent.KEY_DOWN,keyMapping);
}
private function keyMapping(event:KeyboardEvent):void {
if (event.charCode == 13) {
MovieClip(parent).testme.text = "hello world!";
}
}
}
}
The AS3 I have been using on Frame 1 of my main timeline:
stop();
var cloud_mc:cloud = new cloud();
addChild(cloud_mc);
==================================================================================
SUGGESTED FIX
Class:
package {
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
public class cloud extends MovieClip {
public function cloud() {
}
public function tralala() {
stage.addEventListener(KeyboardEvent.KEY_DOWN,keyMapping);
}
private function keyMapping(event:KeyboardEvent):void {
if (event.charCode == 13) {
MovieClip(parent).testme.text = "hello world!";
}
}
}
}
AS3:
stop();
var cloud_mc:cloud = new cloud();
addChild(cloud_mc);
cloud_mc.tralala();
Use the linkage name of the library object, not the class that it extends.
Your library object extends the cloud class which extends movie clip. If you instanciate the cloud class you miss the library object.
The process is described in detail here:
http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b8ea63-7fee.html
Trying to addChild() inside a movie clip in the stage from one of my classes. The code looks like this
package {
import flash.display.SimpleButton;
import flash.events.MouseEvent;
import flash.display.MovieClip;
public class createFlask extends SimpleButton {
public function createFlask() {
addEventListener(MouseEvent.CLICK, createAFlask);
}
private function createAFlask(e:MouseEvent):void
{
var Flask = new flask ;
Flask.x = stage.width/2;
Flask.y = stage.height/2;
stage.experiment.addChild(Flask);
}
}
This gives an error as
Access of possibly undefined property experiment through a reference
with static type flash.display:Stage.
Any solutions?
Just omit "stage".
Instead use
experiment.addChild(Flask);
That will work.