Access of undefined property- but its defined. - actionscript-3

I get that error message for all lines of choosetext()- which is referred every time I change the value of mode.
package
{
import flash.display.Bitmap;
import flash.display.Sprite;
import flash.events.;
import flash.ui.Keyboard;
//import board;
import flash.accessibility.AccessibilityImplementation;
import flash.display.MovieClip;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.display.Sprite;
import flash.utils.ByteArray;
import flash.events.MouseEvent;
import flash.text.AntiAliasType;
import flash.utils.describeType;
import flash.net.;
import Set;
import StatusBox;
import Statusx;
import flash.display.InteractiveObject;
import flash.text.TextFieldType;
import flash.events.FocusEvent;
import fl.managers.FocusManager;
import flash.display.*;
import flash.display.Stage;
import flash.events.KeyboardEvent;
public class boxsprite extends Sprite
{
[Embed(source = "C:/Windows/Fonts/Verdana.ttf", fontName = "Verdana", fontWeight = "bold", advancedAntiAliasing = "true", mimeType = "application/x-font")]
public static const VERD:Class;
[Embed(source="../lib/box.gif")]
private var boxspriteClass:Class
[Embed(source = "../lib/m2.gif")]
private var m2:Class
[Embed(source = "../lib/m3.gif")]
private var m3:Class
[Embed(source="../lib/m4.gif")]
private var m4:Class
[Embed(source = "../lib/m5.gif")]
private var m5:Class
[Embed(source = "../lib/m6.gif")]
private var m6:Class
[Embed(source = "../lib/m7.gif")]
private var m7: Class
[Embed(source="../lib/m8.gif")]
private var m8: Class
[Embed(source = "../lib/m9.gif")]
private var m9: Class
internal var mode:uint=1;
internal var displaytext:String;
internal var setBox:Boolean = false;
internal var onBoard:Array = [0];
internal var playerRound:uint = 1;
internal var round:uint = 1;
internal var playernumber:uint;
internal var myTextBox:TextField = new TextField();
public function boxsprite():void
{
trace (mode);
init2();
var boxsprite2:Bitmap = new boxspriteClass() as Bitmap;
this.addChild(boxsprite2);
var m77:Bitmap = new m7 as Bitmap;
this.addChild(m77)
var m66:Bitmap = new m6 as Bitmap;
this.addChild(m66)
var m55:Bitmap = new m5 as Bitmap;
this.addChild(m55)
var m44:Bitmap = new m4 as Bitmap;
this.addChild(m44)
var m33:Bitmap = new m3 as Bitmap;
this.addChild(m33);
var m22:Bitmap = new m2 as Bitmap;
this.addChild(m22)
}
private function choosetext():void
{ switch (mode)
{
case 2: {this.setChildIndex(m22, this.numChildren - 1);}
case 3: {this.setChildIndex(m33, this.numChildren - 1);}
case 4: {this.setChildIndex(m44, this.numChildren - 1);}
case 5: {this.setChildIndex(m55, this.numChildren - 1);}
case 6: {this.setChildIndex(m66, this.numChildren - 1);}
case 7: {this.setChildIndex(m77, this.numChildren - 1); }
case 8: {this.setChildIndex(m88, this.numChildren - 1); }
case 9: {this.setChildIndex(m99, this.numChildren - 1); }
}
}

Try to define m22,m33...m99 as class variable, not in the boxsprite function
public class boxsprite extends Sprite
{
private var m22:Bitmap;
public function boxsprite():void {
m22= new m2 as Bitmap;
}
}

Related

Totally non-responsive to keyboard presses

This doesn't respond to pressing Enter -traces give no results.
Is there any way to do this that works?
This is just extra ridiculous writing to get past the ludicrous posting restrictions.
Yaddah.
boards.as
package
{
import flash.display.Bitmap;
import flash.display.Sprite;
import flash.events.*;
import flash.ui.Keyboard;
import mx.core.BitmapAsset;
//import board;
import flash.accessibility.AccessibilityImplementation;
import flash.display.MovieClip;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.display.Sprite;
import flash.utils.ByteArray;
import flash.events.MouseEvent;
import flash.text.AntiAliasType;
import flash.utils.describeType;
import flash.net.*;
import Set;
import StatusBox;
import Statusx;
import flash.display.InteractiveObject;
import flash.text.TextFieldType;
import flash.events.FocusEvent;
import fl.managers.FocusManager;
import flash.display.*;
import flash.display.Stage;
import flash.events.KeyboardEvent;
public class boxsprite extends Sprite
{
[Embed(source = "C:/Windows/Fonts/Verdana.ttf", fontName = "Verdana", fontWeight = "bold", advancedAntiAliasing = "true", mimeType = "application/x-font")]
public static const VERD:Class;
[Embed(source="../lib/box.gif")]
private var boxspriteClass:Class
[Embed(source = "../lib/m2.gif")]
private var m2:Class
[Embed(source = "../lib/m3.gif")]
private var m3:Class
[Embed(source="../lib/m4.gif")]
private var m4:Class
[Embed(source = "../lib/m5.gif")]
private var m5:Class
[Embed(source = "../lib/m6.gif")]
private var m6:Class
[Embed(source = "../lib/m7.gif")]
private var m7: Class
[Embed(source="../lib/m8.gif")]
private var m8: Class
[Embed(source = "../lib/m9.gif")]
private var m9: Class
private var m22:Bitmap;
private var m33:Bitmap;
private var m44:Bitmap;
private var m55:Bitmap;
private var m66:Bitmap;
private var m77:Bitmap;
private var m88:Bitmap;
private var m99:Bitmap;
private var boxsprite2:Bitmap;
internal var mode:uint=2;
internal var displaytext:String;
internal var setBox:Boolean = false;
internal var onBoard:Array = [0];
internal var playerRound:uint = 1;
internal var round:uint = 1;
internal var playernumber:uint;
internal var myTextBox:TextField = new TextField();
public function boxsprite():void
{
trace (mode);
boxsprite2=new boxspriteClass() as Bitmap;
this.addChild(boxsprite2);
m22 = new m2 as Bitmap;
this.addChild(m22);
m77= new m7 as Bitmap;
this.addChild(m77);
m66= new m6 as Bitmap;
this.addChild(m66);
m55= new m5 as Bitmap;
this.addChild(m55);
m44= new m4 as Bitmap;
this.addChild(m44);
m33= new m3 as Bitmap;
this.addChild(m33);
this.addEventListener(KeyboardEvent.KEY_DOWN, myKeyDown2);
}
private function myKeyDown2(e2:KeyboardEvent):void
{
if (e2.keyCode == Keyboard.ENTER)
{
trace("Enter pressed");
modeswitch();
}
else if (e2.keyCode ==Keyboard.H)
{
navigateToURL(new URLRequest("http://wp.me/P3FUQl-n"));
}
//else if (e.Keycode == Keyboard.Q)
function modeswitch():void
{ trace(mode);
switch(mode)
{
case 8:
{mode = 9;
choosetext(); }
case 9:
{mode = 2;
choosetext();
}
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
{mode = 3;
choosetext();}
startBox.addEventListener(KeyboardEvent.KEY_DOWN, kill);
This code part in
Board.as- appears to work.
function kill (e2:KeyboardEvent):void
{if (e2.keyCode == Keyboard.NUMBER_1)
{removeChild(this.startBox);
startBox = null;
}
else if (e2.keyCode == Keyboard.NUMBER_2)
{removeChild(this.startBox);
startBox = null;
}
else if (e2.keyCode == Keyboard.NUMBER_3)
{removeChild(this.startBox);
startBox = null;
}
else if (e2.keyCode == Keyboard.NUMBER_4)
{removeChild(this.startBox);
startBox = null;
}
else if (e2.keyCode == Keyboard.NUMBER_5)
{removeChild(startBox);
startBox = null; }
else if (e2.keyCode == Keyboard.NUMBER_6)
{ removeChild(this.board.startBox);
startBox = null;}
else if (e2.keyCode == Keyboard.ENTER)
{removeChild(startBox);
startBox = null;}
Maybe I've give up on keyboard for this part and use mouse events instead- in a previous structure I couldn't have done that as mouse was already occupied doing something but that should be freed up now.
By the way this isn't need on stage initialisation.
Your sprite will never receive any KeyboardEvent. You should add your event listeners directly to the stage. Of course, you have to obtain a link to the stage.
In your constructor delete
this.addEventListener(KeyboardEvent.KEY_DOWN, myKeyDown2);
And add the following:
//Your constructor
...
if(stage)
initKeyboardEvents();
else
addEventListener(Event.ADDED_TO_STAGE, initKeyboardEvents)
//End of constructor
...
//New function
private function initKeyboardEvents(e:Event = null):void
{
stage.addEventListener(KeyboardEvent.KEY_DOWN, myKeyDown2);
}

Child object not detected

Why can't it detect these declared image objects- m22,m33....?
TypeError: 2007 Error #: Parameter child must be non-null.
The code runs fine until choosetext function then errors.
mode traces display.
Other traces display.
Yaddah Yaddah
boxtrans is a transparent sprite to mask mouse input.
package {
import flash.display.Bitmap;
import flash.display.Sprite;
import flash.events.*;
import flash.ui.Keyboard;
import mx.core.BitmapAsset;
//import board;
import flash.accessibility.AccessibilityImplementation;
import flash.display.MovieClip;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.display.Sprite;
import flash.utils.ByteArray;
import flash.events.MouseEvent;
import flash.text.AntiAliasType;
import flash.utils.describeType;
import flash.net.*;
import Set;
import StatusBox;
import Statusx;
import flash.display.InteractiveObject;
import flash.text.TextFieldType;
import flash.events.FocusEvent;
import fl.managers.FocusManager;
import flash.display.*;
import flash.display.Stage;
import flash.events.KeyboardEvent;
import flash.utils.*;
import boxtrans;
public class boxsprite extends Sprite {
[Embed(source = "C:/Windows/Fonts/Verdana.ttf", fontName = "Verdana", fontWeight = "bold", advancedAntiAliasing = "true", mimeType = "application/x-font")]
public static const VERD:Class;
[Embed(source="../lib/box.gif")]
private var boxspriteClass:Class
[Embed(source = "../lib/m2.gif")]
private var m2:Class
[Embed(source = "../lib/m3.gif")]
private var m3:Class
[Embed(source="../lib/m4.gif")]
private var m4:Class
[Embed(source = "../lib/m5.gif")]
private var m5:Class
[Embed(source = "../lib/m6.gif")]
private var m6:Class
[Embed(source = "../lib/m7.gif")]
private var m7: Class
[Embed(source="../lib/m8.gif")]
private var m8: Class
[Embed(source = "../lib/m9.gif")]
private var m9: Class
internal var m22:Bitmap;
internal var m33:Bitmap;
internal var m44:Bitmap;
internal var m55:Bitmap;
internal var m66:Bitmap;
internal var m77:Bitmap;
internal var m88:Bitmap;
internal var m99:Bitmap;
internal var boxsprite2:Bitmap;
internal var boxtrans1:Sprite;
internal var mode:uint=2;
internal var displaytext:String;
internal var setBox:Boolean = false;
internal var onBoard:Array = [0];
internal var playerRound:uint = 1;
internal var round:uint = 1;
internal var playernumber:uint;
internal var myTextBox:TextField = new TextField();
public function boxsprite():void {
init();
}
internal function init():void {
boxsprite2=new boxspriteClass as Bitmap;
this.addChild(boxsprite2);
m77= new m7 as Bitmap;
this.addChild(m77);
m66= new m6 as Bitmap;
this.addChild(m66);
m55= new m5 as Bitmap;
this.addChild(m55);
m44= new m4 as Bitmap;
this.addChild(m44);
m33= new m3 as Bitmap;
this.addChild(m33);
m22 = new m2 as Bitmap;
this.addChild(m22);
boxtrans1 = new boxtrans() as Sprite;
boxtrans1.x = 0;
boxtrans1.y = 240;
this.addChild(boxtrans1);
this.addEventListener(MouseEvent.CLICK, clickDoubleClick);
}
internal var m_nDoubleClickSpeed:Number = 300;
internal var m_toMouse:Number;
internal function clickDoubleClick(e:MouseEvent):void {
if (isNaN(m_toMouse)==false) {
clearTimeout(m_toMouse);
HandleDoubleClick();
} else {
m_toMouse = setTimeout(HandleSingleClick, m_nDoubleClickSpeed);
}
}
internal function HandleSingleClick():void {
trace("HandleSingleClick");
m_toMouse = NaN;
}
internal function HandleDoubleClick():void {
modeswitch();
trace("HandleDoubleClick");
m_toMouse = NaN;
}
internal function modeswitch():void{
trace(mode);
switch(mode) {
case 8:
{mode = 9;
choosetext(); }
case 9:
{mode = 2;
choosetext();
}
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
{mode +=1;
choosetext(); }
}
}
internal function choosetext():void {
switch (mode) {
case 2: {this.setChildIndex(m22,this.numChildren - 1);}
case 3: {this.setChildIndex(m33,this.numChildren - 1);}
case 4: {this.setChildIndex(m44,this.numChildren - 1);}
case 5: {this.setChildIndex(m55,this.numChildren - 1);}
case 6: {this.setChildIndex(m66,this.numChildren - 1);}
case 7: {this.setChildIndex(m77,this.numChildren - 1);}
case 8: {this.setChildIndex(m88,this.numChildren - 1);}
case 9: {this.setChildIndex(m99,this.numChildren - 1); }
}
}
}
}
It looks like you are not instantiating m88 or m99 anywhere.
Since you don't have any breakcommands in your switch statement it falls through and executes every case even if mode is only 2.
I'm assuming you meant to do something like this:
switch (mode) {
case 2:
this.setChildIndex(m22,this.numChildren - 1);
break;
case 3:
this.setChildIndex(m33,this.numChildren - 1);
break;
// etc...
}

Showing main menu over objects / movieclips?

I am creating a game in Flash and I am creating a main menu for the game (With buttons like 'Play', 'How to Play', 'Hiscores' etc.) and was wondering what is the best way to go about it?
All of my Actionscript code is in external .as files and I've used classes throughout but I was having trouble figuring out how to get it so that the menu will be shown as soon as the game is ran. The main problem is that there are timers in my game that have event handlers attached to them and I was trying to think of the best way to essentially stop these timers until the user actually clicks 'Play', otherwise the objects spawn over the top of the menu and the timer ticks down.
Would stopping the timers but then adding an event handler to the play button to start the timers be a good idea? I am trying to figure out the best way to do this for future reference.
Thank you for any assistance.
Edit: Tried Cherniv's advice, getting some errors.
Main.as:
package {
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.display.DisplayObject;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.utils.Timer;
import flash.ui.Mouse;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.text.TextFormat;
import flash.text.TextField;
import flash.display.Loader;
import flash.net.URLRequest;
import flash.net.URLLoader;
import flash.system.LoaderContext;
import flash.display.Sprite;
import flash.net.Socket;
public class Main extends MovieClip {
public static var gameLayer:Sprite = new Sprite;
public static var endGameLayer:Sprite = new Sprite;
public static var menuLayer:Sprite = new Sprite;
public var gameTime:int;
public var levelDuration:int;
public var crosshair:crosshair_mc;
static var score:Number;
var enemyShipTimer:Timer;
var enemyShipTimerMed:Timer;
var enemyShipTimerSmall:Timer;
static var scoreHeader:TextField = new TextField();
static var scoreText:TextField = new TextField();
static var timeHeader:TextField = new TextField();
static var timeText:TextField = new TextField();
public function Main()
{
var mainMenu:myMenu = new myMenu;
addChild(gameLayer);
addChild(endGameLayer);
addChild(menuLayer);
playBtn.addEventListener(MouseEvent.CLICK, startButtonPressed);
}
function startButtonPressed(e:Event)
{
levelDuration = 30;
gameTime = levelDuration;
var gameTimer:Timer = new Timer(1000,levelDuration);
gameTimer.addEventListener(TimerEvent.TIMER, updateTime);
gameTimer.addEventListener(TimerEvent.TIMER_COMPLETE, timeExpired)
gameTimer.start();
scoreHeader = new TextField();
scoreHeader.x = 5;
scoreHeader.text = String("Score: ");
gameLayer.addChild(scoreHeader);
scoreText = new TextField();
scoreText.x = 75;
scoreText.y = 0;
scoreText.text = String(0);
gameLayer.addChild(scoreText);
timeHeader = new TextField();
timeHeader.x = 490;
timeHeader.y = 0;
timeHeader.text = String("Time: ");
gameLayer.addChild(timeHeader);
timeText = new TextField();
timeText.x = 550;
timeText.y = 0;
timeText.text = gameTime.toString();
gameLayer.addChild(timeText);
var scoreFormat = new TextFormat("Arial Rounded MT Bold", 20, 0xFFFFFF);
scoreHeader.setTextFormat(scoreFormat);
scoreText.setTextFormat(scoreFormat);
timeHeader.setTextFormat(scoreFormat);
timeText.setTextFormat(scoreFormat);
enemyShipTimer = new Timer(2000);
enemyShipTimer.addEventListener("timer", sendEnemy);
enemyShipTimer.start();
enemyShipTimerMed = new Timer(2500);
enemyShipTimerMed.addEventListener("timer", sendEnemyMed);
enemyShipTimerMed.start();
enemyShipTimerSmall = new Timer(2750);
enemyShipTimerSmall.addEventListener("timer", sendEnemySmall);
enemyShipTimerSmall.start();
crosshair = new crosshair_mc();
gameLayer.addChild(crosshair);
crosshair.mouseEnabled = crosshair.mouseChildren = false;
Mouse.hide();
gameLayer.addEventListener(Event.ENTER_FRAME, moveCursor);
resetScore();
}
function sendEnemy(e:Event)
{
var enemy = new EnemyShip();
gameLayer.addChild(enemy);
gameLayer.addChild(crosshair);
}
function sendEnemyMed(e:Event)
{
var enemymed = new EnemyShipMed();
gameLayer.addChild(enemymed);
gameLayer.addChild(crosshair);
}
function sendEnemySmall(e:Event)
{
var enemysmall = new EnemyShipSmall();
gameLayer.addChild(enemysmall);
gameLayer.addChild(crosshair);
}
static function updateScore(points)
{
score += points;
scoreText.text = String(score);
var scoreFormat = new TextFormat("Arial Rounded MT Bold", 20, 0xFFFFFF);
scoreHeader.setTextFormat(scoreFormat);
scoreText.setTextFormat(scoreFormat);
}
static function resetScore()
{
score = 0;
scoreText.text = String(score);
}
function updateTime(e:TimerEvent):void
{
trace(gameTime);
// your class variable tracking each second,
gameTime--;
//update your user interface as needed
var scoreFormat = new TextFormat("Arial Rounded MT Bold", 20, 0xFFFFFF);
timeText.defaultTextFormat = scoreFormat;
timeText.text = String(gameTime);
}
function timeExpired(e:TimerEvent):void
{
var gameTimer:Timer = e.target as Timer;
gameTimer.removeEventListener(TimerEvent.TIMER, updateTime)
gameTimer.removeEventListener(TimerEvent.TIMER, timeExpired)
// do whatever you need to do for game over
}
function moveCursor(event:Event)
{
crosshair.x=mouseX;
crosshair.y=mouseY;
}
}
}
Menu.as:
package {
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.display.DisplayObject;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.utils.Timer;
import flash.ui.Mouse;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.text.TextFormat;
import flash.text.TextField;
import flash.display.Loader;
import flash.net.URLRequest;
import flash.net.URLLoader;
import flash.system.LoaderContext;
import flash.display.Sprite;
import flash.net.Socket;
public class Menu extends MovieClip
{
var mainMenu:Menu = new Menu();
Main.menuLayer.addChild(myMenu);
playBtn.addEventListener(MouseEvent.CLICK, playBtnPressed);
function playBtnPressed()
{
Main.menuLayer.removeChild(myMenu);
dispatchEvent(new Event("playButtonPressed"))
}
}
My menu is a movieclip named myMenu and the class is set as Menu but I get errors such as:
Main.as, Line 50 1120: Access of undefined property playBtn
I gave the button an instance name of playBtn before I converted the menu to a movieclip so not sure what's going on there. I'm probably missing something really easy but it's a bit confusing for me after typing all day.
If you have all of your initialization stuff (including timers initializations) in Main constructor function , so you need to split it to two functions , Main constructor will show the menu , and "start" button will fire the second function , that will include all the initialization stuff

Something wrong with my code

I have a fla file and an external class called theAlarmClock.
The hands on the clock should be moving, but after Ctrl+Enter - they are idle.
package
{
import flash.display.MovieClip;
import flash.events.TimerEvent;
import flash.utils.Timer;
public class theAlarmClock extends MovieClip
{
var myDate:Date;
var mySec:int;
var myMin:int;
var myHou:int;
var myTimer:Timer = new Timer(1000);
public function theAlarmClock()
{
myTimer.addEventListener(TimerEvent.TIMER, updateClock);
myTimer.start();
function updateClock(t:TimerEvent):void
{
myDate = new Date();
mySec = myDate.seconds;
myMin = myDate.minutes;
myHou = myDate.hours;
clockHandSeconds_mc.rotation = mySec * 6;
clockHandMinutes_mc.rotation = myMin * 6;
clockHandHours_mc.rotation = myHou * 30 + myMin * 0.5;
trace("tick");
}
}
}
}
package {
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.TimerEvent;
import flash.sensors.Accelerometer;
import flash.utils.Timer;
public class ToolClass extends Sprite
{
private var myDate:Date;
private var mySec:uint;
private var myMin:uint;
private var myHou:uint;
private var myClock:Sprite;
private var myHour:Shape;
private var myMins:Shape;
private var mySeco:Shape;
private var myTimer:Timer=new Timer(1000);
public function ToolClass()
{
drawClock();
myTimer.addEventListener(TimerEvent.TIMER,updateClock);
myTimer.start();
}
private function drawClock():void
{
myClock=new Sprite();
myClock.graphics.lineStyle(0,0x000000,1);
myClock.graphics.drawCircle(0,0,100);
addChild(myClock);
myClock.x=150;
myClock.y=150;
myHour=new Shape();
myHour.graphics.lineStyle(5,0x00000,1);
myHour.graphics.lineTo(0,-50);
myClock.addChild(myHour);
myMins=new Shape();
myMins.graphics.lineStyle(2,0x000000,1);
myMins.graphics.lineTo(0,-80);
myClock.addChild(myMins);
mySeco=new Shape();
mySeco.graphics.lineStyle(1,0x000000,1);
mySeco.graphics.lineTo(0,-80);
myClock.addChild(mySeco);
}
public function getTime():void
{
myDate=new Date();
myHou=myDate.hours;
myMin=myDate.minutes;
mySec=myDate.seconds;
}
private function updateClock(e:TimerEvent):void
{
getTime();
myHour.rotation=myHou*30+30*myMin/60;
myMins.rotation=myMin*6+6*mySec/60;
mySeco.rotation=mySec*6;
trace("tick");
}
}
}
You should try the get-functions of the Date-class.
Try this code in your updateClock function instead:
myDate = new Date();
mySec = myDate.getSeconds();
myMin = myDate.getMinutes();
myHou = myDate.getHours();
clockHandSeconds_mc.rotation = mySec * 6;
clockHandMinutes_mc.rotation = myMin * 6;
clockHandHours_mc.rotation = myHou * 30 + myMin * 0.5;
trace("tick");

3D object in FLARToolKit keep appearing even though there is no marker detected

I have another (newbie) question about FLARToolKit.
This is Lee Brimelow's code to make 3 boxes on top of the marker.
package {
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.events.Event;
import flash.media.Camera;
import flash.media.Video;
import flash.utils.ByteArray;
import org.libspark.flartoolkit.core.FLARCode;
import org.libspark.flartoolkit.core.param.FLARParam;
import org.libspark.flartoolkit.core.raster.rgb.FLARRgbRaster_BitmapData;
import org.libspark.flartoolkit.core.transmat.FLARTransMatResult;
import org.libspark.flartoolkit.detector.FLARSingleMarkerDetector;
import org.libspark.flartoolkit.support.pv3d.FLARBaseNode;
import org.libspark.flartoolkit.support.pv3d.FLARCamera3D;
import org.papervision3d.lights.PointLight3D;
import org.papervision3d.materials.shadematerials.FlatShadeMaterial;
import org.papervision3d.materials.utils.MaterialsList;
import org.papervision3d.objects.primitives.Cube;
import org.papervision3d.render.BasicRenderEngine;
import org.papervision3d.scenes.Scene3D;
import org.papervision3d.view.Viewport3D;
[SWF(width="640", height="480", frameRate="30", backgroundColor="#FFFFFF")]
public class FLARdemo extends Sprite
{
[Embed(source="pat1.pat", mimeType="application/octet-stream")]
private var pattern:Class;
[Embed(source="camera_para.dat", mimeType="application/octet-stream")]
private var params:Class;
private var fparams:FLARParam;
private var mpattern:FLARCode;
private var vid:Video;
private var cam:Camera;
private var bmd:BitmapData;
private var raster:FLARRgbRaster_BitmapData;
private var detector:FLARSingleMarkerDetector;
private var scene:Scene3D;
private var camera:FLARCamera3D;
private var container:FLARBaseNode;
private var vp:Viewport3D;
private var bre:BasicRenderEngine;
private var trans:FLARTransMatResult;
public function FLARdemo()
{
setupFLAR();
setupCamera();
setupBitmap();
setupPV3D();
addEventListener(Event.ENTER_FRAME, loop);
}
private function setupFLAR():void
{
fparams = new FLARParam();
fparams.loadARParam(new params() as ByteArray);
mpattern = new FLARCode(16, 16);
mpattern.loadARPatt(new pattern());
}
private function setupCamera():void
{
vid = new Video(640, 480);
cam = Camera.getCamera();
cam.setMode(640, 480, 30);
vid.attachCamera(cam);
addChild(vid);
}
private function setupBitmap():void
{
bmd = new BitmapData(640, 480);
bmd.draw(vid);
raster = new FLARRgbRaster_BitmapData(bmd);
detector = new FLARSingleMarkerDetector(fparams, mpattern, 80);
}
private function setupPV3D():void
{
scene = new Scene3D();
camera = new FLARCamera3D(fparams);
container = new FLARBaseNode();
scene.addChild(container);
var pl:PointLight3D = new PointLight3D();
pl.x = 1000;
pl.y = 1000;
pl.z = -1000;
var ml:MaterialsList = new MaterialsList({all: new FlatShadeMaterial(pl)});
var Cube1:Cube = new Cube(ml, 30, 30, 30);
var Cube2:Cube = new Cube(ml, 30, 30, 30);
Cube2.z = 50
var Cube3:Cube = new Cube(ml, 30, 30, 30);
Cube3.z = 100
container.addChild(Cube1);
container.addChild(Cube2);
container.addChild(Cube3);
bre = new BasicRenderEngine();
trans = new FLARTransMatResult();
vp = new Viewport3D;
addChild(vp);
}
private function loop(e:Event):void
{
bmd.draw(vid);
try
{
if(detector.detectMarkerLite(raster, 80) && detector.getConfidence() > 0.5)
{
detector.getTransformMatrix(trans);
container.setTransformMatrix(trans);
bre.renderScene(scene, camera, vp);
}
}
catch(e:Error){}
}
}
the flaw of this code is, after the marker detected, yes, the 3D object got rendered. but after the marker were not detected, the object still appeared on the screen (even though it did not move)
how can I fix this?
you have to change the visible property of the FLARBaseNode in the detection condition
if(detector.detectMarkerLite(raster, 80) && detector.getConfidence() > 0.5){
detector.getTransformMatrix(trans);
container.setTransformMatrix(trans);
container.visible = true;
bre.renderScene(scene, camera, vp);
}else
container.visible = false;