tweenlite not working with bitmaps? - actionscript-3

I have the following problem when I try to use TweenLite with a Bitmap:
I get strange error messages when applying TweenLite to a Bitmap (tempScore.bitmap). GetBounds works. The bitmap has transparency. Does anyone have an idea why it doesn't work? Any help appreciated. Thanks.:)
When using a getBounds-Method I get this:
tempScore.bitmap.getBounds(this)(x=2.35, y=-0.45, w=25, h=18)
This is the error message:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at com.greensock.plugins::TransformAroundPointPlugin/onInitTween()
at com.greensock.plugins::TransformAroundCenterPlugin/onInitTween()
at com.greensock::TweenLite/init()
at com.greensock::TweenLite/renderTime()
at com.greensock.core::SimpleTimeline/renderTime()
at com.greensock::TweenLite$/updateAll()
My imported and activated libraries look like this:
import com.greensock.*;
import com.greensock.TweenLite;
import com.greensock.plugins.TweenPlugin;
import com.greensock.plugins.TransformAroundCenterPlugin;
import com.greensock.plugins.TransformAroundPointPlugin;
import com.greensock.easing.*;
import com.greensock.plugins.AutoAlphaPlugin;
import com.greensock.plugins.ColorTransformPlugin;
import com.greensock.plugins.ColorMatrixFilterPlugin;
TweenPlugin.activate([TransformAroundCenterPlugin, TransformAroundPointPlugin, ColorTransformPlugin,
ColorMatrixFilterPlugin]);
This is the part where I try to use TweenLite on my tempScore:
var scoreTextLength:int = scoreManager.scores.length - 1;
for (var counter:int = scoreTextLength; counter >= 0; counter--)
{
tempScore = scoreManager.scores[counter];
tempScore.startDelay = true;
TweenLite.to(tempScore.bitmap, 2, {transformAroundCenter: {scale:2}});
trace("tempScore.bitmap.getBounds(this)" + tempScore.bitmap.getBounds(this));
if (tempScore.update())
{
disposeScore(counter);
}
}
So far as I can see the getBounds-values are ok. My game is based on a gameframework. There's a renderer inside of it.
Call me an idiot if I'm wrong but is it possible that the renderer of the framework and tweenlite are getting in each other's way??
TweenLite has problems with other similar objects like tempAsteroid. A lot of objects are drawn onto a canvas with copyPixels (blitting-method).
tempScore is a Score-object. The Score object is based on a BasicBlitArrayObject. Otherwise this object extends an EventDispatcher. I hope this little info helps.
This is the scoreManager which manages the look and properties of tempScore:
package com.cosmicward.src.classes
{
import flash.display.*;
import flash.text.*;
import flash.geom.*;
import com.framework_mod.src.BlitArrayAsset;
public class ScoreManager
{
public var scoreBitmapData:BitmapData;
public var scoreBitmap:Bitmap;
public var scoreAnimationFrames:Array = [];
public var scores:Array;
public var tempScore:Score;
private var textfield:TextField = new TextField();
private var textFormat:TextFormat = new TextFormat();
private var $textWidth:int;
private var $textHeight:int;
private var rec:Rectangle;
public var scoreCount:int;
public var scoreCountTwo:int;
public var scoreCountThree:int;
private var drawingCanvas:Shape = new Shape();
private var point0:Point = new Point(0, 0);
public function ScoreManager()
{
}
public function createScoreLook(textWidth:int, textHeight:int, text:String, textFormat:TextFormat):void {
var tempBlitArrayAsset:BlitArrayAsset = new BlitArrayAsset();
scoreBitmapData = new BitmapData(textWidth, textHeight, true, 0x00000000);
var font:ArialBold = new ArialBold();
textFormat.font = "ArialBold";
textFormat.font = font.fontName;
Font.registerFont(ArialBold);
textfield.embedFonts = true;
textfield.blendMode = BlendMode.LAYER;
//textfield.autoSize = TextFieldAutoSize.LEFT;
textfield.defaultTextFormat = textFormat;
textfield.setTextFormat(textFormat);
textfield.selectable = false;
textfield.text = text;
trace("drawingCanvas.height =" + drawingCanvas.height);
trace("drawingCanvas.width =" + drawingCanvas.width);
scoreBitmapData.draw(textfield);/
$textWidth = textWidth;
$textHeight = textHeight;
//*** end look
}
public function createScores(xPos:Number, yPos:Number, stopAnimation:int = 5,
scoreDelay:int = 10, scoreLife:int = 40):void {
var tempScore:Score = new Score(5, 1315, 5, 995);
tempScore.bitmapData = scoreBitmapData;
scoreBitmap = new Bitmap(tempScore.bitmapData);
tempScore.bitmap = scoreBitmap;
tempScore.x = xPos;
tempScore.y = yPos;
tempScore.life = scoreLife;
tempScore.lifeCount = 0;
tempScore.widthObject = $textWidth;
tempScore.heightObject = $textHeight;
tempScore._delay = scoreDelay;
tempScore.delayCount = 0;
tempScore.nextX = tempScore.x;
tempScore.nextY = tempScore.y;
scores.push(tempScore);
}
}
}
Here is some code of the BasicBlitArrayObject (tempScore rests upon that (i.e. Score-object):
package com.framework_mod.src
{
import flash.display.BitmapData;
import flash.geom.Point;
import flash.events.EventDispatcher;
import flash.geom.Rectangle;
import flash.display.Bitmap;
public class BasicBlitArrayObject extends EventDispatcher{
public var x:Number = 0;
public var y:Number = 0;
public var nextX:Number = 0;
public var nextY:Number = 0;
public var dx:Number = 0;
public var dy:Number = 0;
public var frame:int = 0;
public var bitmapData:BitmapData;
public var bitmap:Bitmap;
public var animationList:Array = [];
public var testList:Array = [];
public var point:Point = new Point(0, 0);
public var speed:Number = 0;
public var xMax:int = 0;
public var yMax:int = 0;
public var xMin:int = 0;
public var yMin:int = 0;
public var aniType:int = 1;
public var health:int = 0;
public var _size:int = 0;
public var score:int = 0;
public var _damage:int = 0;
public var count:int = 0;
public var bitmapSize:int = 0;
public var life:int = 0;
public var lifeCount:int = 0;
public var startCount:Boolean;
public var _delay:int = 0;
public var delayCount:int = 0;
public var startDelay:Boolean;
public var _stop:int;
public var stopAni:Boolean;
public var stopAniCount:int = 0;
public var _type:int = 0;
public var shield:int = 0;
public var healthPoints:int = 0;
public var widthObject:int;
public var heightObject:int;
public var boost:Number;
public var boostLfe:int;
public var weaponLfe:int;
public var _projXAdjust:Number;
public var _projYAdjust:Number;
public var number:int;
public var _offset:int;
public var marked:Boolean;
public var objMove:Boolean;
public var removeObj:Boolean;
public var finished:Boolean;
public function BasicBlitArrayObject(xMin:int, xMax:int, yMin:int, yMax:int)
{
this.xMin = xMin;
this.xMax = xMax;
this.yMin = yMin;
this.yMax = yMax;
//trace("basicblittarrayobject");
}
public function updateFrame(inc:int, aniType:int = 1):void
{
frame += inc;
switch (aniType) {
case 1:
if (frame > animationList.length - 1){
frame = 0;
}
bitmapData = animationList[frame];
break;
case 2:
if (frame > animationList.length - 1){
frame = 0;
}
bitmapData = animationList[1][frame];
break;
}
}
public function render(canvasBitmapData:BitmapData):void {
x = nextX;
y = nextY;
point.x = x;
point.y = y;
canvasBitmapData.copyPixels(bitmapData, bitmapData.rect, point);
}
public function dispose():void {
bitmapData.dispose();
bitmapData = null;
bitmap = null;
animationList = null;
point = null;
}
}
}

I think the problem is simple, but the resolution is not. The objects you are trying to transform are blitted. So they are not added to the stage. So the object's stage property is null.
I suspect that the TransformAroundPoint plugin is trying to use the object's stage property, and that is throwing a null object error your are seeing.
To see a simple example of this, make a very simple file.
Create two bitmaps, add one to the stage, and don't add the other.
Apply the tween to the stage instance, it will work.
Then apply the tween to the off-stage instance, and you should get the same error you get in the game.
What you will need to do instead is handle the transform yourself. Instead of using TweenLite to rotate around a point, do it yourself.
Fortunately Stack Overflow already has a great thread on that topic!
Rotate around a point that is not (0,0)

Related

Error 1119 in Actionscript 3 (as3) Access of possibly undefined property text through a reference with static type money_txt

This is in the main class
package {
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.display.Sprite;
import flash.events.Event;
public class main extends MovieClip {
public var scene = 0;
public var _money = 0;
public var gain = 1;
public var clicks = 0;
public function main() {
addEventListener(Event.ENTER_FRAME, loop);
mainbtn.addEventListener(MouseEvent.CLICK, handler);
playbtn.addEventListener(MouseEvent.CLICK, playHandler);
}
var mainbtn:button = new button();
var playbtn:playbutton = new playbutton();
var playtxt:playtext = new playtext();
var cash:money_txt = new money_txt();
var scene0:MovieClip = new MovieClip();
var scene1:MovieClip = new MovieClip();
public function loop(e:Event):void {
if(scene == 0) {
addChild(scene0)
scene0.addChild(playbtn);
playbtn.x = 300;
playbtn.y = 200;
scene0.addChild(playtxt);
playtxt.x = 300;
playtxt.y = 100;
} else {
scene0.removeChild(playbtn);
scene0.removeChild(playtxt);
}
if(scene == 1) {
addChild(scene1);
scene1.addChild(mainbtn);
mainbtn.x = 300;
mainbtn.y = 200;
scene1.addChild(cash);
cash.text = 'Money: ' + _money.toString();
} else {
scene1.removeChild(mainbtn);
}
}
public function playclickHandler(e:MovieClip) {
scene = 1;
}
public function handler(e:MouseEvent):void {
_money += gain;
clicks++;
trace('yep');
}
public function playHandler(e:MouseEvent):void {
scene = 1;
}
}
}
And This is where the error would be
C:\Users\Slime\Desktop\Art-ish\game\main.as, Line 47, Column 10 1119: Access of possibly undefined property text through a reference with static type money_txt.
Thanks for helping if you can!
these should be defined as public
public var mainbtn:button = new button();
public var playbtn:playbutton = new playbutton();
public var playtxt:playtext = new playtext();
public var cash:money_txt = new money_txt();
public var scene0:MovieClip = new MovieClip();
public var scene1:MovieClip = new MovieClip();
also it is hard to tell if money_txt, playtext, playbutton and button are classes or MovieClip instances. Convention dictates that Classes should start with a capital letter and instances with lower.
update
The issue is that if button and playbutton are buttons and playtext and money_txt are MovieClips, you should instantiate them as such.
for example if you have
public var mainbtn:button = new button();
but there is no class with name of button, mainbtn will be null. What you may need to do is
public var mainbtn:Button;
public var cash:MovieClip;
and as a part of your main or some other function, assign the instances
mainbtn = this['button'];
cash = this['money_txt'];
you can check if this worked by checking trace(cash);, which will return null if the assignment did not work.
I should stress again though, it is hard to to know what exactly is going wrong without knowing what your setup is. I'm assuming money_txt and the other classes you are defining are not actually classes with their own linkage IDs, but buttons and movieclips inside the MovieClip or stage you are putting this code in.

ActionScript 3 Apparently I cannot access movieclip

I've created a zoom function but when I try to scale the bg_image nothing happends. It is like I cannot access it's properties. Does anyone know why? :)
Ty!
Main class
package
{
import flash.display.MovieClip;
import flash.utils.Dictionary;
import flash.events.MouseEvent;
import flash.display.Sprite;
import flash.text.TextField;
import fl.controls.Button;
import fl.controls.List;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.events.Event;
import flash.display.Shape;
import fl.transitions.Fly;
import fl.motion.MatrixTransformer;
import flash.events.KeyboardEvent;
import flash.geom.Matrix;
import flash.geom.Point;
import flash.ui.Mouse;
public class Main extends Sprite
{
// Zoom
public static var scale:Number = 1;
public var spImage:Sprite;
public var mat:Matrix;
public var mcIn:MovieClip;
public var mcOut:MovieClip;
public var boardWidth:int = 980;
public var boardHeight:int = 661;
public var boardMask:Shape;
public var externalCenter:Point;
public var internalCenter:Point;
public const scaleFactor:Number = 0.8;
public var minScale:Number = 0.25;
public var maxScale:Number = 10.0;
// ------------------------
// Grafikk
public var bg_image:Sprite;
//-------------------------------
//-----------------------------------------------
public var routeArray:Array;
public var startList:List = new List();
public var sluttList:List = new List();
public var S_Norway:Dictionary = new Dictionary();
public var S_Australia:Dictionary = new Dictionary();
public var S_China:Dictionary = new Dictionary();
public var S_South_Africa:Dictionary = new Dictionary();
public var S_Brazil:Dictionary = new Dictionary();
public var S_USA:Dictionary = new Dictionary();
public var S_France:Dictionary = new Dictionary();
// ------------------------------------------------------
public static var airportDict:Dictionary = new Dictionary();
public function Main()
{
addEventListener(Event.ADDED_TO_STAGE, init);
// ---------------------------------
}
public function init(e:Event):void
{
bg_image = new Image(0, 0);
this.addChild(bg_image);
bg_image.addEventListener(MouseEvent.CLICK, mouseCoordinates);
removeEventListener(Event.ADDED_TO_STAGE, init);
// Zoom
this.graphics.beginFill(0xB6DCF4);
this.graphics.drawRect(0,0,boardWidth,boardHeight);
this.graphics.endFill();
spImage = new Sprite();
this.addChild(spImage);
boardMask = new Shape();
boardMask.graphics.beginFill(0xDDDDDD);
boardMask.graphics.drawRect(0,0,boardWidth,boardHeight);
boardMask.graphics.endFill();
boardMask.x = 0;
boardMask.y = 0;
this.addChild(boardMask);
spImage.mask = boardMask;
minScale = boardWidth / bg_image.width;
mcIn = new InCursorClip();
mcOut = new OutCursorClip();
bg_image.addChild(mcIn);
bg_image.addChild(mcOut);
bg_image.scaleX = minScale;
bg_image.scaleY = minScale;
spImage.addChild(bg_image);
spImage.addChild(mcIn);
spImage.addChild(mcOut);
spImage.addEventListener(MouseEvent.MOUSE_DOWN, startDragging);
stage.addEventListener(MouseEvent.MOUSE_UP, stopDragging);
spImage.addEventListener(MouseEvent.CLICK, zoom);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, keyHandler);
S_USA["x"] = 180.7;
S_USA["y"] = 149.9;
S_USA["bynavn"] = "New York";
S_Norway["x"] = 423.7;
S_Norway["y"] = 76.4;
S_Norway["bynavn"] = "Oslo";
S_South_Africa["x"] = -26;
S_South_Africa["y"] = 146;
S_South_Africa["bynavn"] = "Cape Town";
S_Brazil["x"] = 226;
S_Brazil["y"] = 431.95;
S_Brazil["bynavn"] = "Rio de Janeiro";
S_France["x"] = 459.1;
S_France["y"] = 403.9;
S_France["bynavn"] = "Paris";
S_China["x"] = 716.2;
S_China["y"] = 143.3;
S_China["bynavn"] = "Beijing";
S_Australia["x"] = 809.35;
S_Australia["y"] = 414.95;
S_Australia["bynavn"] = "Sydney";
// ----------------------------------------------------
airportDict["USA"] = S_USA;
airportDict["Norway"] = S_Norway;
airportDict["South Africa"] = S_South_Africa;
airportDict["Brazil"] = S_Brazil;
airportDict["France"] = S_France;
airportDict["China"] = S_China;
airportDict["Australia"] = S_Australia;
for (var k:Object in airportDict)
{
var value = airportDict[k];
var key = k;
startList.addItem({label:key, data:key});
sluttList.addItem({label:key, data:key});
var airport:Airport = new Airport(key,airportDict[key]["bynavn"]);
airport.koordinater(airportDict[key]["x"], airportDict[key]["y"]);
bg_image.addChild(airport);
}
// --------------------------------------------
// --------------------------------------------
// -----------------------------------------------
}
private function startDragging(mev:MouseEvent):void
{
spImage.startDrag();
}
private function stopDragging(mev:MouseEvent):void
{
spImage.stopDrag();
}
private function zoom(mev:MouseEvent):void
{
if ((!mev.shiftKey)&&(!mev.ctrlKey))
{
return;
}
if ((mev.shiftKey)&&(mev.ctrlKey))
{
return;
}
externalCenter = new Point(spImage.mouseX,spImage.mouseY);
internalCenter = new Point(bg_image.mouseX,bg_image.mouseY);
if (mev.shiftKey)
{
bg_image.scaleX = Math.max(scaleFactor*bg_image.scaleX, minScale);
bg_image.scaleY = Math.max(scaleFactor*bg_image.scaleY, minScale);
}
if (mev.ctrlKey)
{
trace("Minscale: ", maxScale)
trace("Returned: ", 1/scaleFactor*bg_image.scaleY)
bg_image.scaleX = Math.min(1/scaleFactor*bg_image.scaleX, maxScale);
bg_image.scaleY = Math.min(1/scaleFactor*bg_image.scaleY, maxScale);
}
mat = this.transform.matrix.clone();
MatrixTransformer.matchInternalPointWithExternal(mat,internalCenter,externalCenter);
bg_image.transform.matrix = mat;
}
private function keyHandler(ke:KeyboardEvent):void
{
mcIn.x = spImage.mouseX;
mcIn.y = spImage.mouseY;
mcOut.x = spImage.mouseX;
mcOut.y = spImage.mouseY;
mcIn.visible = ke.ctrlKey;
mcOut.visible = ke.shiftKey;
if (ke.ctrlKey || ke.shiftKey)
{
Mouse.hide();
}
else
{
Mouse.show();
}
}
private function reise(evt:MouseEvent):void
{
var new_flight:Flight = new Flight(airportDict[startList.selectedItem.label]["x"],airportDict[startList.selectedItem.label]["y"],airportDict[sluttList.selectedItem.label]["x"],airportDict[sluttList.selectedItem.label]["y"]);
bg_image.addChild(new_flight);
}
private function mouseCoordinates(event: MouseEvent):void
{
// these are the x and y relative to the object
var localMouseX:Number = bg_image.mouseX;
var localMouseY:Number = bg_image.mouseY;
trace("Local coordinates: ", localMouseX, localMouseY);
// these are the x and y relative to the whole stage
var stageMouseX:Number = event.stageX;
var stageMouseY:Number = event.stageY;
trace("Global coordinates: ", stageMouseX, stageMouseY);
}
}
}
Image class:
package {
import flash.display.Sprite;
import flash.display.MovieClip;
public class Image extends Sprite
{
public function Image(y_:Number, x_:Number)
{
this.y = y_
this.x = x_
}
}
}
You don't have minScale declared, neither maxScale.
bg_image should be declared as Sprite, otherwise Compiler Error: Implicit coercion...
`
public var bg_image:Sprite;
As your image is very small, you may want to add your listener to the stage, and not the image, it would be very dificult to click in such a small image.
Also, here is your example working with MOUSE_WHEEL, instead of Click + Ctrl / Shift
stage.addEventListener(MouseEvent.MOUSE_WHEEL, zoom);
private function zoom(mev:MouseEvent):void
{
mev.delta > 0 ?
bg_image.scaleX = bg_image.scaleY = Math.max(scaleFactor * bg_image.scaleX, minScale) :
bg_image.scaleX = bg_image.scaleY = Math.min(1/scaleFactor * bg_image.scaleX, maxScale) ;
}

Null object reference actionscript 3

i make a class like this:
package
{
import flash.display.Sprite;
import flash.text.TextField;
public class Hint extends Sprite
{
public static var _instance:Hint = null;
public function Hint()
{
_instance = this;
}
public function DrawHintText():void
{
Const._scoreText = new TextField();
Const._scoreTextHolder = new TextField();
Const._highScoreText = new TextField();
Const._highScoreTextHolder = new TextField();
Const._timeLeft = new TextField();
Const._timeLeftHolder = new TextField();
Const._scoreTextHolder.textColor = 0xFFFFFF;
Const._scoreTextHolder.x = stage.stageWidth - 350;
Const._scoreTextHolder.y = 100;
Const._scoreTextHolder.text = "Score: ";
Const._scoreTextHolder.selectable = false;
Const._scoreText.textColor = 0xFFFFFF;
Const._scoreText.x = stage.stageWidth - 250;
Const._scoreText.y = 100;
Const._scoreText.text = "--";
Const._scoreText.selectable = false;
Const._highScoreTextHolder.textColor = 0xFFFFFF;
Const._highScoreTextHolder.x = stage.stageWidth - 350;
Const._highScoreTextHolder.y = 150;
Const._highScoreTextHolder.text = "High Score: ";
Const._highScoreTextHolder.selectable = false;
Const._highScoreText.textColor = 0xFFFFFF;
Const._highScoreText.x = stage.stageWidth - 250;
Const._highScoreText.y = 150;
Const._highScoreText.text = "--";
Const._highScoreText.selectable = false;
Const._timeLeftHolder.textColor = 0xFF0000;
Const._timeLeftHolder.x = stage.stageWidth - 350;
Const._timeLeftHolder.y = 200;
Const._timeLeftHolder.text = "Time Left: ";
Const._timeLeftHolder.selectable = false;
Const._timeLeft.textColor = 0xFF0000;
Const._timeLeft.x = stage.stageWidth - 275;
Const._timeLeft.y = 200;
Const._timeLeft.text = "00:00";
Const._timeLeft.selectable = false;
addChild(Const._scoreText);
addChild(Const._scoreTextHolder);
addChild(Const._highScoreText);
addChild(Const._highScoreTextHolder);
addChild(Const._timeLeft);
addChild(Const._timeLeftHolder);
}
}
}
and i called on the GameManager:
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.text.TextField;
[SWF(width='1366',height='768',backgroundColor='#000000',frameRate='30')]
public class GameManager extends Sprite
{
public function GameManager():void
{
DrawHintText();
GenerateField();
ShowField();
GenerateGems();
}
private function GenerateField():void
{
Const._gridField = new Array();
for (var i:uint = 0; i < Const._gridSizeY; i++)
{
Const._gridField[i] = new Array();
for (var j:uint = 0; j < Const._gridSizeX; j++)
{
Const._gridField[i][j] = 0;
}
}
}
private function ShowField():void
{
Const._fieldSprite = new Sprite();
addChild(Const._fieldSprite);
Const._fieldSprite.graphics.lineStyle(1, 0xFFFFFF);
for (var i:uint = 0; i < Const._gridSizeY; i++)
{
for (var j:uint = 0; j < Const._gridSizeX; j++)
{
Const._fieldSprite.graphics.beginFill(0x666666);
Const._fieldSprite.graphics.drawRect(25 + 65 * j, 80 + 60 * i, 65, 60);
Const._fieldSprite.graphics.endFill();
}
}
}
private function DrawHintText():void
{
Hint._instance.DrawHintText();
}
private function GenerateGems():void
{
}
}
}
and here is the Const:
package
{
import flash.display.Sprite;
import flash.text.TextField;
public class Const
{
public static var _gridField:Array;
public static var _fieldSprite:Sprite;
public static var _scoreText:TextField;
public static var _scoreTextHolder:TextField;
public static var _highScoreText:TextField;
public static var _highScoreTextHolder:TextField;
public static var _timeLeft:TextField;
public static var _timeLeftHolder:TextField;
public static const _gridSizeX:Number = 10;
public static const _gridSizeY:Number = 10;
public function Const()
{
}
}
}
when i run the code, i got the error:
Where do i miss some code?
Invoking function DrawHintText() in Hint._instance.DrawHintText(); doesn't create instance of class Hint.
Try to substite your public variable with public getter.
private static var __instance:Hint = null;
public static function get _instance():Hint {
if (!__instance) {
__instance = new Hint();
}
return __instance;
}
And drop constructor, because getter makes it unnecessary.
Might be a silly question, but are you actually creating instance of Hint somewhere?
Because as long as you don't do new Hint() that Hint._instance will be null.
Try something like:
public class Hint extends Sprite
{
private static var _instance:Hint;
public function Hint()
{
if (_instance) throw new Error("Hint... use getInstance()");
_instance = this;
}
public static function getInstance():Hint
{
if (!_instance) new Hint();
return _instance;
}
//DrawHintText >> rename to drawHintText
public function drawHintText():void
{
//your code here
}
}
And to use:
Hint.getInstance().drawHintText();
Use Flash Builder, which costs some money, or Flash Develop. which is free. Both will show you compile time errors, with line numbers and location file.
Also at runtime will show you line number and the .as file where you have the problem.
Also autocomplete cuts your coding time by at least half time.

Actionscript 3.0 I'm trying to figure out how to access properties of event target

I'm trying to figure out how to reference the class of a target. Here is some of the code:
xmlDoc = new XML(xmlLoader.data);
//trace(xmlDoc.Video[1].Desc);
for (var i:int = 0; i < xmlDoc.Video.length(); i++)
{
xmlObj = new FilmVideo(xmlDoc.Video[i].Name, xmlDoc.Video[i].title, xmlDoc.Video[i].Thumb, xmlDoc.Video[i].URL, xmlDoc.Video[i].APILoader);
XMLItem[i] = xmlObj;
//trace(XMLItem);
MovieClip(root).main_mc.thumb_mc.addChild(XMLItem[i]);
if (i <= 0) {
XMLItem[i].x = 20;
XMLItem[i].y = 0;
} else if (i > 0){
XMLItem[i].x = XMLItem[i-1].x + XMLItem[i-1].width + 120;
XMLItem[i].y = 0;
}
XMLItem[i].addEventListener(MouseEvent.CLICK, makeThumbClick);
XMLItem[i].addEventListener(MouseEvent.MOUSE_OVER, makeThumbRollOver);
XMLItem[i].addEventListener(MouseEvent.ROLL_OUT, makeThumbRollOut);
}
}
function makeThumbClick(e:MouseEvent)
{
//var myFilmVideo:FilmVideo = FilmVideo(e.target);
MovieClip(root).main_mc.play();
trace(FilmVideo(e.target));
/MovieClip(root).main_mc.theater_mc.videoLoader(FilmVideo(e.target)._APILoad, FilmVideo(e.target)._videoURL);
}
The XMLItem is an array that's storing a class object I custom made (the class name is FilmVideo based off movieclip). The _thumbToMC is a method within my custom class that returns a movieclip. The class has info stored within its properties I would like to pass through a function called in the makeThumbClick function. However, I have no idea how. e.target reference the _thumbToMC movieclip rather than the class. I do I reference the class? Thank you in advance :)
Here is the class:
package filmvideo
{
import flash.display.Loader;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.net.URLRequest;
public class FilmVideo extends MovieClip
{
public var _nameXML:String = "";
public var _title:String = "";
public var _thumbURL:URLRequest;
public var _videoURL:URLRequest;
public var _APILoad:String = "";
public var loader:Loader = new Loader();
public function FilmVideo(name:String, title:String, thumbURL:String, videoURL:String, APILoad:String)
{
_nameXML = name;
_title = title;
_thumbURL = new URLRequest(thumbURL);
_videoURL = new URLRequest(videoURL);
_APILoad = APILoad;
//trace(_name);
//trace(_title);
//trace(thumbURL);
//trace(videoURL);
//trace(_APILoad);
this.addChild(loader);
loader.load(_thumbURL);
}
}
}
You could simplify your class to:
package filmvideo
{
import flash.display.Loader;
import flash.display.MovieClip;
import flash.net.URLRequest;
public class FilmVideo extends MovieClip
{
public var _nameXML:String = "";
public var _title:String = "";
public var _thumbURL:URLRequest;
public var _videoURL:URLRequest;
public var _APILoad:String = "";
public var loader:Loader = new Loader();
public function FilmVideo(name:String, title:String, thumbURL:String, videoURL:String, APILoad:String)
{
_nameXML = name;
_title = title;
_thumbURL = new URLRequest(thumbURL);
_videoURL = new URLRequest(videoURL);
_APILoad = APILoad;
//trace(_name);
//trace(_title);
//trace(thumbURL);
//trace(videoURL);
//trace(_APILoad);
this.addChild(loader);
loader.load(_thumbURL);
}
}
}
Then you can use FilmVideo as a MovieClip (since it extends the MovieClip class).
And use 'currentTarget' instead of 'target', because it always points to the listened object, while 'target' points to the object which fired the event. more info here
xmlDoc = new XML(xmlLoader.data);
//trace(xmlDoc.Video[1].Desc);
for (var i:int = 0; i < xmlDoc.Video.length(); i++)
{
xmlObj = new FilmVideo(xmlDoc.Video[i].Name, xmlDoc.Video[i].title, xmlDoc.Video[i].Thumb, xmlDoc.Video[i].URL, xmlDoc.Video[i].APILoader);
XMLItem[i] = xmlObj;
//trace(XMLItem);
Object(root).main_mc.thumb_mc.addChild(XMLItem[i]);
if (i <= 0) {
XMLItem[i].x = 20;
XMLItem[i].y = 0;
} else if (i > 0){
XMLItem[i].x = XMLItem[i-1].x + XMLItem[i-1].width + 120;
trace(XMLItem[i].width);
XMLItem[i].y = 0;
}
XMLItem[i].addEventListener(MouseEvent.CLICK, makeThumbClick);
XMLItem[i].addEventListener(MouseEvent.MOUSE_OVER, makeThumbRollOver);
XMLItem[i].addEventListener(MouseEvent.ROLL_OUT, makeThumbRollOut);
}
function makeThumbClick(e:MouseEvent)
{
//var myFilmVideo:FilmVideo = FilmVideo(e.target);
MovieClip(root).main_mc.play();
trace(myFilmVideo._APILoad);
MovieClip(root).main_mc.theater_mc.videoLoader(FilmVideo(e.currentTarget)._APILoad, FilmVideo(e.currentTarget)._videoURL);
}
If you don't want to do this way
Add a reference (inside the class) to the _thumbToMC back to it's FilmVideo object.
_thumbMC._filmVideo = this;
Then:
function makeThumbClick(e:MouseEvent)
{
//var myFilmVideo:FilmVideo = FilmVideo(e.target);
MovieClip(root).main_mc.play();
MovieClip(root).main_mc.theater_mc.videoLoader(MovieClip(e.currentTarget)._filmVideo._APILoad, MovieClip(e.currentTarget)._filmVideo._videoURL);
}
I'm not 100% sure I understand the question, but assuming you want to retrieve the properties of the FilmVideo instance (which it appears the user is clicking on) then maybe this is what you are looking for;
function makeThumbClick(e:MouseEvent){
var myFilmVideo:FilmVideo = FilmVideo(e.target);
// access properties of _thumbToMC
myFilmVideo.randomproperty = 123;
}

Using variable from another class as3

i separated one big file into multiple file to have it cleaned and i got a problem now.
I have my main.as, character.as, camera.as.
What i'm trying to do is access a variable from another class which i set later on that class. Ill show you what i mean.
From my main.as im loading each class and add them as child so it get displayed on screen.
public function buildGame()
{
var loadMap:Sprite = new nf_MapBuilder();
var xChar:Sprite = new nf_Character();
var xCam:Sprite = new nf_Camera();
var UserControl:nf_UserControl = new nf_UserControl();
addChild(loadMap);
addChild(xChar);
addChild(xCam);
addChild(UserControl);
}
Everything show on screen like it needed. Then it goes to my character.as:
package as3
{
import flash.display.Sprite;
import flash.events.Event;
public class nf_Character extends Sprite
{
public var character_pos:Array = new Array();
public var character_is_moving:Boolean = false;
public var character_x_dir:int = 0;
public var character_y_dir:int = 0;
public var character:hero = new hero();
public function nf_Character()
{
addEventListener(Event.ADDED_TO_STAGE,xCharLoad);
}
public function xCharLoad(e:Event)
{
character_pos = [2,2];
character.x=64*(character_pos[1]);
character.y=64*(character_pos[0]);
addChild(character);
}
}
}
There is the problem. I need to use those variable i set there in my character.as to use it in my camera.as:
package as3
{
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Rectangle;
import flash.display.StageScaleMode;
import as3.nf_Character;
public class nf_Camera extends Sprite
{
private var xChar:nf_Character = new nf_Character();
//Camera variables
var stageW2:Number;
var stageH2:Number;
var view:Rectangle;
public function nf_Camera()
{
addEventListener(Event.ADDED_TO_STAGE,xCamGo);
}
public function xCamGo(e:Event):void
{
trace("Camera pos - " + xChar.x + " " + xChar.character.y);
view = new Rectangle(0,0,stage.stageWidth,stage.stageHeight)
stageW2 = stage.stageWidth / 2 - 32;
stageH2 = stage.stageHeight / 2 - 32;
addEventListener(Event.ENTER_FRAME,CamView);
}
public function CamView(e:Event):void
{
view.x = xChar.character.x - stageW2;
view.y = xChar.character.y - stageH2;
scrollRect = view;
}
}
}
When it was all in one big file it was ok i just had to set the variable in the class and acessing it trough every function but now im kinda confused. Anyone see how i could do this?
In short, I think you should subscribe to an event from your character in your main class which is fired whenever the character moves. In the handler for that event you could call a method on the camera to set it's position according to the current position of the character.
main.as
private var xChar:Sprite = new nf_Character();
private var xCam:Sprite = new nf_Camera();
public function buildGame()
{
var loadMap:Sprite = new nf_MapBuilder();
var UserControl:nf_UserControl = new nf_UserControl();
// listen for when the character has moved
xChar.addEventListener(MoveEvent.MOVED, characterMovedHandler);
addChild(loadMap);
addChild(xChar);
addChild(xCam);
addChild(UserControl);
}
private function characterMovedHandler(event:MoveEvent):void
{
xCam.setPosition(xChar.x, xChar.y);
}
nf_Character.as
public class nf_Character extends Sprite
{
public var character_pos:Array = new Array();
public var character_is_moving:Boolean = false;
public var character_x_dir:int = 0;
public var character_y_dir:int = 0;
public var character:hero = new hero();
public function nf_Character()
{
addEventListener(Event.ADDED_TO_STAGE,xCharLoad);
}
public function xCharLoad(e:Event)
{
character_pos = [2,2];
character.x=64*(character_pos[1]);
character.y=64*(character_pos[0]);
addChild(character);
}
public function xCharMoved()
{
// Dispatch a custom event when the character moves
dispatchEvent(new MovedEvent(MovedEvent.MOVED));
}
}
nf_Camera.as
public class nf_Camera extends Sprite
{
private var xChar:nf_Character = new nf_Character();
//Camera variables
var stageW2:Number;
var stageH2:Number;
var view:Rectangle;
public function nf_Camera()
{
addEventListener(Event.ADDED_TO_STAGE,xCamGo);
}
public function xCamGo(e:Event):void
{
trace("Camera pos - " + xChar.x + " " + xChar.character.y);
view = new Rectangle(0,0,stage.stageWidth,stage.stageHeight)
stageW2 = stage.stageWidth / 2 - 32;
stageH2 = stage.stageHeight / 2 - 32;
// Probably only need one enterframe either in your character class or main
//addEventListener(Event.ENTER_FRAME,CamView);
}
public function setPosition(x:Number, y:Number):void
{
view.x = xChar.character.x - stageW2;
view.y = xChar.character.y - stageH2;
scrollRect = view;
}
}
Out of interest, how are you moving the character?
You can pass your character class instance to your camera class instance as an argument of the constructor. You will then have a reference to the character inside the camera class and you can access it's variables
// Inside buildGame() in main.
var xChar:nf_Character = new nf_Character();
var xCam:nf_Camera = new nf_Camera(xChar);
// Inside nf_Camera
public function nf_Camera(char:nf_Character) {
xChar = char;
}