In my class, if I create bitmapData like this:
private var tImage:BitmapData;
public function object():void {
tImage = new BitmapData(30,30,false,0x000000);
}
I get the following error:
ArgumentError: Error #2015: Invalid BitmapData.
But if I declare the variable inside the method:
public function object():void {
var tImage:BitmapData;
tImage = new BitmapData(30,30,false,0x000000);
}
It works fine. WHY!?!?! It's driving me crazy.
Thanks guys!
I think it might be some other code in your class.
The following works, but I didn't name the function "object" (since I'm guessing that's a reserved word??)
package
{
/**
* ...
* #author your name here
*/
import flash.display.MovieClip;
import flash.events.Event;
import flash.display.Bitmap;
public class TestBitmap extends MovieClip
{
private var tImage:BitmapData;
public function TestBitmap():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
tImage = new BitmapData(30,30,false,0x000000);
}
}
}
This simplified version below also works too:
package
{
/**
* ...
* #author your name here
*/
import flash.display.MovieClip;
import flash.events.Event;
import flash.display.Bitmap;
public class TestBitmap extends MovieClip
{
private var tImage:BitmapData;
public function TestBitmap():void
{
tImage = new BitmapData(30,30,false,0x000000);
}
}
}
You declared tImage as private...
private var tImage:BitmapData;
public function object():void {
tImage = new BitmapData(30,30,false,0x000000);
}
Its should be
var tImage:BitmapData;
public function object():void {
tImage = new BitmapData(30,30,false,0x000000);
}
Derp
Related
I have this class which should take a button from stage (xBtn).
package com.stx.utils {
import flash.display.MovieClip;
import flash.events.*;
public class STXbutonx extends MovieClip {
private var xBtn : MovieClip;
public function STXbutonx() {
addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event=null) : void {
trace("int!"); //this is never called
xBtn = stage.getChildByName('ics') as MovieClip;
xBtn.addEventListener(MouseEvent.CLICK, onX);
removeEventListener(Event.ADDED_TO_STAGE, init);
}
private function onX(e:MouseEvent) : void {
trace("x Clicked!");
}
}
}
and in Document class I called like this :
import flash.display.MovieClip;
import com.stx.utils.*;
public class Main extends MovieClip {
var xx : STXbutonx;
public function Main() {
xx = new STXbutonx();
}
}
Why my init function is never called?
Thank you!
Because you never add it to the stage.
Change your Document class to
public function Main() {
xx = new STXbutonx();
addChild(xx);
}
Main holds the reference to the stage, adding a child to Main will add the child to the stage. Thus the event listener in STXbutonx will fire.
xBtn = stage.getChildByName('ics') as MovieClip; // now I have acces of undefined property stage...
You don't have access to the stage because STXButon is not on the stage, it is not an DisplayObject. To get around this, do this:
package com.stx.utils {
import flash.display.MovieClip;
import flash.events.*;
public class STXbutonx{
private var xBtn : MovieClip;
private var stage : Stage;
public function STXbutonx(stage:Stage) {
this.stage = stage;
init();
}
private function init() : void {
trace("int!");
xBtn = stage.getChildByName('ics') as MovieClip;
xBtn.addEventListener(MouseEvent.CLICK, onX);
removeEventListener(Event.ADDED_TO_STAGE, init);
}
private function onX(e:MouseEvent) : void {
trace("x Clicked!");
}
}
}
And of course
import flash.display.MovieClip;
import com.stx.utils.*;
public class Main extends MovieClip {
var xx : STXbutonx;
public function Main() {
xx = new STXbutonx(stage);
}
}
I have the simplest code ever. Main class:
package
{
import field.Field;
import flash.display.Sprite;
import flash.events.Event;
public class Main extends Sprite
{
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
var field:Field = new Field();
addChild(field);
field.test();
}
}
}
and a Field class:
package field
{
import flash.display.Sprite;
public class Field extends Sprite
{
public function Field()
{
super();
}
public function test():void
{
}
}
}
test method is presented.
But when I try to compile I get this:
Main.as(26): col: 10 Error: Call to a possibly undefined method test.
field.test();
How could this be happening?
field is your package, that's why you can not do field.test(). So you have to choose another name of your Field instance. You can do like this :
var _field:Field = new Field();
addChild(_field);
_field.test();
Hope that can help.
I'm new to AS3 from Java and was trying to implement a Java style as3 mvc implementation which essentially has two views with an input field and text box which has it's contents changed by buttons.
I keep getting Error: Call to a possibly undefined method handleMouseClick through a reference with static type controller:Controller. and can't understand why. This is for the function handleMouseClick in TextToolsView
Here is my code (sorry for it being so long, I'm not sure how to condense it further without losing my error):
TextModel
package model
{
import flash.events.Event;
import flash.events.EventDispatcher;
public class TextModel extends EventDispatcher
{
private var text:String = new String();
private var initialText:String = new String("Initial Text");
public function TextModel()
{
setText(initialText);
}
public function setText(text:String):void {
this.text = text;
}
public function getText():String {
return this.text;
}
public function updateText(text:String):void {
setText(text);
dispatchEvent(new Event(Event.CHANGE));
}
public function clearText():void {
setText("Text has been cleared");
dispatchEvent(new Event(Event.CHANGE));
}
public function resetText():void {
setText(initialText);
dispatchEvent(new Event(Event.CHANGE));
}
}
}
TextController
package controller
{
import flash.events.MouseEvent;
import model.TextModel;
public class TextController extends AbstractController
{
/**
* Constructor
* #param m model to modify
*/
public function TextController(m:TextModel)
{
super(m);
}
private function updateText(text:String):void {
TextModel(getModel()).updateText(text);
}
private function clearText():void {
TextModel(getModel()).clearText();
}
private function resetText():void {
TextModel(getModel()).resetText();
}
/*override public function update(obj: Object) {
}*/
public function handleMouseClick(event:MouseEvent):void {
switch(event.currentTarget.id) {
case "_updateButton":
updateText("TEXT INPUT TO BE ADDED LATER");//add text input later
break;
case "_clearButton":
clearText();
break;
case "_resetButton":
resetText();
break;
}
}
}
}
AbstractController
package controller
{
import model.TextModel;
import view.View;
/**
* Provides basic services for the "controller" of
* a Model/View/Controller triad.
*
*/
public class AbstractController
{
private var model:TextModel;
private var view:View;
public function AbstractController(m:TextModel)
{
setModel(m);
}
public function setModel(m:TextModel):void
{
model = m;
}
public function getModel():TextModel
{
return model;
}
public function setView(v:View):void
{
view = v;
}
public function getView():View
{
return view;
}
public function update(obj:Object):void {
}
}
}
TextBoxView
package view
{
import controller.Controller;
import flash.display.Sprite;
import flash.events.Event;
import flash.text.TextField;
import flash.text.TextFormat;
import model.TextModel;
import model.TextUpdate;
/**
* ...
*/
public class TextBoxView extends AbstractView {
private var wrapper:Sprite = new Sprite();
private var textBox:TextField = new TextField();
private var inputField:TextField = new TextField();
public function TextBoxView(m:TextModel, c:Controller, x:int, y:int) {
super(m, c);
textBox.text = "This is a text Panel with lots of text!!!!!!!!!!!!dghsdfghdfghdfghdfghdfghdfghdfghdfghdfghdfghdfghdfghdfgh";
textBox.border = true;
textBox.borderColor = 0x000000;
textBox.multiline = true;
textBox.width = 425;
textBox.height = 115;
textBox.x = 145;
textBox.y = 20;
textBox.wordWrap = true;
var myFormat:TextFormat = new TextFormat();
myFormat.color = 0xAA0000;
myFormat.size = 24;
myFormat.italic = true;
//myFormat.align = TextFormatAlign.CENTER
textBox.setTextFormat(myFormat);
addEventListener(Event.CHANGE, this.update);
//inputable text box
inputField.border = true;
inputField.width = 200;
inputField.height = 150;
inputField.x = 200;
inputField.y = 50;
inputField.type = "input";
inputField.multiline = true;
wrapper.addChild(textBox);
wrapper.addChild(inputField);
addChild(wrapper);
}
public function update(event:Event):void {
textBox.text = super.getModel().getText();
}
}
}
TextToolsView
package view
{
import controller.Controller;
import flash.events.Event
import controller.TextController;
import flash.display.Sprite;
import flash.events.MouseEvent;
import model.TextModel;
import model.TextUpdate;
import ui.CustomButton;
public class TextToolsView extends AbstractView
{
private var updateButton:CustomButton;
private var clearButton:CustomButton;
private var resetButton:CustomButton;
private var wrapper:Sprite = new Sprite();
private var textModel:TextModel;
private var textController:TextController;
public function TextToolsView(m:TextModel, c:Controller, x:int, y:int) {
super(m, c);
makeTools(x, y);
}
override public function defaultController (model:TextModel):Controller {
return new TextController(model);
}
private function makeTools(x:int, y:int):void {
updateButton = new CustomButton("update", "_updateButton", 100, 22);
updateButton.x = 0;
updateButton.y = 0;
updateButton.addEventListener(MouseEvent.CLICK, handleMouseClick);
clearButton = new CustomButton("clear", "_clearButton", 100, 22);
clearButton.x = 120;
clearButton.y = 0;
clearButton.addEventListener(MouseEvent.CLICK, handleMouseClick);
resetButton = new CustomButton("reset", "_resetButton", 100, 22);
resetButton.x = 240;
resetButton.y = 0;
resetButton.addEventListener(MouseEvent.CLICK, handleMouseClick);
wrapper.x = x;
wrapper.y = y;
wrapper.addChild(updateButton);
wrapper.addChild(clearButton);
wrapper.addChild(resetButton);
addChild(wrapper);
}
private function handleMouseClick(event:MouseEvent):void {
super.getController().handleMouseClick(event);
}
}
}
Abstract View
package view
{
import controller.Controller;
import flash.display.Sprite;
import model.TextModel;
/**
* Provides basic services for the "view" of
* a Model/View/Controller triad.
*/
public class AbstractView extends Sprite
{
private var model:TextModel;
private var controller:Controller;
public function AbstractView(m:TextModel, c:Controller)
{
setModel(m);
setController(c);
}
/**
* returns the default controller for this view
*/
public function defaultController (model:TextModel):Controller {
return null;
}
/**
* Sets the model this view is observing.
*/
public function setModel (m:TextModel):void {
model = m;
}
/**
* Returns the model this view is observing.
*/
public function getModel ():TextModel {
return model;
}
/**
* Sets the controller for this view.
*/
public function setController (c:Controller):void {
controller = c;
// Tell the controller this object is its view.
getController().setView(this);
}
/**
* Returns this view's controller.
*/
public function getController():Controller {
return controller;
}
}
}
I won't post the custom button because essentially it works fine like any normal button.
and Main
package
{
import controller.TextController;
import flash.display.Sprite;
import flash.events.Event;
import model.TextModel;
import view.TextBoxView;
import view.TextToolsView;
/**
* ...
*/
public class Main extends Sprite
{
private var text_model:TextModel;
private var text_box:TextBoxView;
private var text_tools:TextToolsView;
private var textController:TextController;
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
text_model = new TextModel();
textController = new TextController(text_model);
text_box = new TextBoxView(text_model, textController, 0,0);
//text_model.addObserver(text_box);
text_tools = new TextToolsView(text_model, textController, 120, 300);
//text_model.addObserver(text_tools);
addChild(text_box);
addChild(text_tools);
}
}
}
I don't see code of Controller, but I think problem is linked to it.
super.getController().handleMouseClick(event);
In this code getController() method returns Controller instance, but this class doesn't contain method handleMouseClick(). So, I think, you need to cast Controller to TextController like this:
(getController() as TextController).handleMouseClick(event);
i am making a simple game. here is the problem i am facing, but first i will tell you my class structure.
(i am using flash cs5.5)
Enemy.as : this class is linked with a MovieClip(in library), having code of simple enemy movment and directions.
Hero.as : Linked with a MovieClip in library. Code of Hero simple Movment
EnemyManager.as : Creates new enemy Every 20 Second.
HeroManager.as : Creates Hero(Only Once, other functionality will be added later).
HittingManager.as : checks for collusions(Problem Here)
Now My Problem is in HittingManager.as class because i want to add HitTestObject Functionalty in this class. i will post code of 3 important classes. (EnemyManager.as, HeroManager.as, HittingManager.as )
package
{
import flash.display.MovieClip;
import flash.events.Event;
public class HeroManager extends MovieClip
{
private var hManager:HittingManager = new HittingManager();
public static var hero:Hero = new Hero();
public function HeroManager()
{
addEventListener(Event.ADDED_TO_STAGE, added);
}
private function added(event:Event):void
{
trace("hero manager added");
}
}//class
}//package
Here is the code of EnemyManager.as class
package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.sampler.Sample;
import flash.display.Sprite;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.sampler.NewObjectSample;
public class EnemyManager extends MovieClip
{
private var hManager:HittingManager = new HittingManager();
private var timer:Timer = new Timer(2000);
public static var hitting:Boolean = false;
public static var enemy:Enemy;
public function EnemyManager()
{
addEventListener(Event.ADDED_TO_STAGE, added);
}
public function addEnemy(newEnemy:Enemy):void
{
addChild(newEnemy);
hManager.registerEnemy(newEnemy);
}
private function added(event:Event):void
{
addEventListener(Event.ENTER_FRAME, update);
trace("added enemy manger");
timer.addEventListener(TimerEvent.TIMER, onTimer);
timer.start();
}
private function onTimer(event:TimerEvent):void
{
enemy = new Enemy();
this.addEnemy(enemy);
}
private function update(event:Event):void
{
}
}
}
And here Hittest.as class (i have tried many techniqes but all in vain ) so i am leaving if statment empty
package
{
import flash.display.MovieClip;
import flash.events.Event;
public class HittingManager extends MovieClip
{
private var eManager:EnemyManager;
private var hEnemy:Enemy = new Enemy();
private var _enemies:Array;
private var _hero:Hero;
public function HittingManager()
{
//trace("Hitting Manager working");
_enemies = [];
addEventListener(Event.ADDED_TO_STAGE, added);
}
public function registerEnemy(newEnemy:Enemy):void
{
_enemies.push(newEnemy);
}
public function registerHero():void
{
//trace("heroRegisterd");
_hero = HeroManager.hero;
addChild(_hero);
}
private function added(event:Event):void
{
if(!_hero)
{
this.registerHero();
}
trace("Hitting manger added");
addEventListener(Event.ENTER_FRAME, update);
}
private function update(event:Event):void
{
if(_hero)
{
for each( newEnemy:Enemy in _enemies)
{
if(_hero.hitTestObject(newEnemy) )
{
trace("Hitting")
}
}
}
}
}//class
}//package
The revision below gets your code working with as few changes as possible, however it is in no way an ideal solution.
You should probably refactor your code so that the enemy manager doesn't need a public static reference to the HittingManager
package
{
import flash.display.MovieClip;
import flash.events.Event;
public class HeroManager extends MovieClip
{
public static var hero:Hero = new Hero();
public function HeroManager()
{
addEventListener(Event.ADDED_TO_STAGE, added);
}
private function added(event:Event):void
{
trace("hero manager added");
}
} //class
} //package
package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.sampler.Sample;
import flash.display.Sprite;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.sampler.NewObjectSample;
public class EnemyManager extends MovieClip
{
public static var hManager:HittingManager;
private var timer:Timer = new Timer(2000);
public static var hitting:Boolean = false;
public static var enemy:Enemy;
public function EnemyManager()
{
addEventListener(Event.ADDED_TO_STAGE, added);
}
public function addEnemy(newEnemy:Enemy):void
{
addChild(newEnemy);
hManager.registerEnemy(newEnemy);
}
private function added(event:Event):void
{
addEventListener(Event.ENTER_FRAME, update);
trace("added enemy manger");
timer.addEventListener(TimerEvent.TIMER, onTimer);
timer.start();
}
private function onTimer(event:TimerEvent):void
{
enemy = new Enemy();
this.addEnemy(enemy);
}
private function update(event:Event):void
{
}
}
}
package
{
import flash.display.MovieClip;
import flash.events.Event;
public class HittingManager extends MovieClip
{
private var _enemies:Array;
private var _hero:Hero;
public function HittingManager()
{
//trace("Hitting Manager working");
_enemies = [];
addEventListener(Event.ADDED_TO_STAGE, added);
}
public function registerEnemy(newEnemy:Enemy):void
{
_enemies.push(newEnemy);
}
public function registerHero():void
{
//trace("heroRegisterd");
_hero = HeroManager.hero;
addChild(_hero);
}
private function added(event:Event):void
{
if (!_hero)
{
this.registerHero();
}
trace("Hitting manger added");
addEventListener(Event.ENTER_FRAME, update);
}
private function update(event:Event):void
{
if (_hero)
{
for each (var newEnemy:Enemy in _enemies)
{
if (_hero.hitTestObject(newEnemy))
{
trace("Hitting")
}
}
}
}
} //class
} //package
In your main document class, you should have something like:
var hittingManager:HittingManager = new HittingManager();
EnemyManager.hManager = hittingManager;
var enemyManager:EnemyManager = new EnemyManager();
var heroManager:HeroManager = new HeroManager();
addChild(heroManager);
addChild(enemyManager);
addChild(hittingManager);
Function X1 simply does not run- no trace result and programme so basic.
Board.as is called- I checked.
Simple sprite display not working.
Main.as
package
{
import flash.display.Bitmap;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.*;
import flash.text.TextField;
import flash.ui.Keyboard;
import Start;
import Board;
/**
* ...
* #author Michael
*/
public class Main extends Sprite
{
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init():void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
var Board1:Sprite = new Board();
stage.addChild(Board1);
Board1.visible = true;
var Start1:Sprite = new Start();
Start1.x = 32;
Start1.y = 32;
addChild (Start1);
stage.addEventListener(KeyboardEvent.KEY_DOWN, myKeyDown);
function myKeyDown(e:KeyboardEvent):void{
if (e.keyCode ==Keyboard.SPACE)
{
removeChild(Start1);
Start1 = null;
}
}
}
Board.as
package
{
import flash.display.Bitmap;
import flash.display.Graphics;
import flash.display.Sprite;
/**
* ...
* #author Michael
*/
public class Board extends Sprite
{
[Embed(source="../lib/Board.jpg")]
private var BoardClass :Class
public function X1():void
{
var boardclass:Bitmap = new BoardClass () as Bitmap;
trace("Project is running fine!");
this.addChild(boardclass);
}
}
}
You are not calling the function X1(). Change the code part in your Main.as class where you create the Board to this:
var Board1:Sprite = new Board();
Board1.X1();
stage.addChild(Board1);
A few more tips for your code:
1) You don't need Board1.visible = true;, it's visible by default
2) Change the name of Board1 to board1 or just board. It's a standard to call classes with first capital letter.
EDIT:
If you want X1() to be run as you create the object, call this function in the constructor of Board.as. Constructor is a function that is run when you create an object. For Board.as it would be like this:
public function Board():void
{
X1(); // this function will be called when you create a new Board object
}