save store on Android device with AS3 - actionscript-3

I'm trying to do a "save" and "restore" function for my game on Android (Adobe AIR).
So far, It seems to work for saving the game, but for I've got an error for the loading.
Here's what I did,
public function save(e){
//Save data in local memory
if(useShared){
shared = SharedObject.getLocal("myApp");
shared.data[saveNum] = jsonSave;
shared.flush();
}
private function restore(e){
if (useShared){
// Get data from local memory
shared = SharedObject.getLocal("myApp");
if(shared.data[saveNum]){
trace("Restoring: " + shared.data[saveNum]);
allSaveData = new Object;
allSaveData = JSON.stringify(shared.data[saveNum]);
}
}
Between I put all the information needed (player's current location...etc).
Did I make a mistake in my functions ?
Thank you for your help,
EDIT :
If I click on "save" everything seems to work, but if then I can't load my game. In debug mode I've got this error :
ReferenceError: Error #1069: Property datetime not found on String and there is no default value.
at com.laserdragonuniversity.alpaca::SaveRestore/populateSaves()[C:\Users\Stéphan\Desktop\18 aout\La Brousse en folie tactile\com\laserdragonuniversity\alpaca\SaveRestore.as:76]
at com.laserdragonuniversity.alpaca::SaveRestore/showSaver()[C:\Users\Stéphan\Desktop\18 aout\La Brousse en folie tactile\com\laserdragonuniversity\alpaca\SaveRestore.as:149]
at com.laserdragonuniversity.alpaca::Toolbar/showSaver()[C:\Users\Stéphan\Desktop\18 aout\La Brousse en folie tactile\com\laserdragonuniversity\alpaca\Toolbar.as:129]
Here's all my code for the save/restore function.
package com.laserdragonuniversity.alpaca {
import flash.events.*;
import com.adobe.serialization.json.*;
import flash.display.Stage;
import flash.display.MovieClip;
import flash.net.SharedObject;
import flash.net.*;
import flash.display.Loader;
import flash.utils.getDefinitionByName;
import flash.text.TextField;
import flash.text.TextFormat;
public class SaveRestore extends MovieClip {
private var inv:Inventory;
private var puzzle:Puzzle;
private var player:Player;
private var back:Background;
private var options:Options;
private var stageRef:Stage;
private var shared:SharedObject;
private var useExternalFile:Boolean = false; //Set this to false if you don't want to deal with server-side scripts
private var useShared:Boolean = true; // Set this to false if you don't want to use Flash player's local memory
private var exLoader:URLLoader;
private var exReq:URLRequest;
private var saveURL:String;
private var saveID:String;
private var userID:String;
private var checkDialog:MovieClip;
private var slotsTaken:Array = new Array(false, false, false);
private var currentEvent;
private var confirm:MovieClip;
private var allSaveData:Object;
public function SaveRestore(stageRef:Stage, saveURL:String, saveID:String) {
this.stageRef = stageRef;
this.saveURL = saveURL;
this.saveID = saveID;
switchSave.gotoAndStop("on");
switchRestore.gotoAndStop("off");
// Un-comment this if you want save data to be cleared each time the SWF is opened
// Probably only necessary for testing purposes
//shared = SharedObject.getLocal(saveID);
//shared.clear();
populateSaves();
restore1.visible = false;
restore2.visible = false;
restore3.visible = false;
checkDialog = new saveOverwrite;
addChild(checkDialog);
checkDialog.visible = false;
addEventListener(Event.ADDED_TO_STAGE, setUpListeners);
}
public function populateSaves(){
if(useExternalFile){
// Here's where you can grab data from external .js files if you choose. This will require the use of server-side scripts
} else if(useShared){
shared = SharedObject.getLocal("myApp");
var saveNum:String;
var shotNum:String;
for (var i = 1; i < 4; i++){
saveNum = "save" + i;
if (shared.data[saveNum]){
slotsTaken[i-1] = true;
var thisSave = JSON.stringify(shared.data[saveNum]);
this[saveNum].savedetail.text = thisSave.datetime;
this["restore"+i].savedetail.text = thisSave.datetime;
// Use a MovieClip of the background as a screenshot
var bg:Object = fakeScreenshot(thisSave.playerLoc.room);
this["save"+i].addChild(bg.bg);
this["save"+i].addChild(bg.bgmask);
var bg2:Object = fakeScreenshot(thisSave.playerLoc.room);
this["restore"+i].addChild(bg2.bg);
this["restore"+i].addChild(bg2.bgmask);
}
}
}
}
public function setUpListeners(e){
switchSave.addEventListener(MouseEvent.CLICK, gotoSave, false, 0, true);
switchSave.buttonMode = true;
switchRestore.addEventListener(MouseEvent.CLICK, gotoRestore, false, 0, true);
switchRestore.buttonMode = true;
save1.btn.addEventListener(MouseEvent.CLICK, overwriteCheck, false, 0, true);
save2.btn.addEventListener(MouseEvent.CLICK, overwriteCheck, false, 0, true);
save3.btn.addEventListener(MouseEvent.CLICK, overwriteCheck, false, 0, true);
restore1.btn.addEventListener(MouseEvent.CLICK, restore, false, 0, true);
restore2.btn.addEventListener(MouseEvent.CLICK, restore, false, 0, true);
restore3.btn.addEventListener(MouseEvent.CLICK, restore, false, 0, true);
checkDialog.canceller.addEventListener(MouseEvent.CLICK, cancelled, false, 0, true);
checkDialog.saver.addEventListener(MouseEvent.CLICK, overwrite, false, 0, true);
closer.addEventListener(MouseEvent.CLICK, closeSaver, false, 0, true);
addEventListener("closeThis", closeSaver);
}
public function gotoSave(e){
gotoAndStop("save");
switchSave.gotoAndStop("on");
switchRestore.gotoAndStop("off");
restore1.visible = false;
restore2.visible = false;
restore3.visible = false;
save1.visible = true;
save2.visible = true;
save3.visible = true;
}
public function gotoRestore(e){
gotoAndStop("restore");
switchSave.gotoAndStop("off");
switchRestore.gotoAndStop("on");
restore1.visible = true;
restore2.visible = true;
restore3.visible = true;
save1.visible = false;
save2.visible = false;
save3.visible = false;
}
public function showSaver(){
if(checkDialog.visible){
checkDialog.visible = false;
}
populateSaves();
visible = true;
Engine.playerControl = false;
gotoSave(null);
}
public function overwriteCheck(e){
currentEvent = e;
var parentBtn = e.target.parent.name;
var slotNum = parentBtn.substr(parentBtn.length-1, 1);
if (slotsTaken[slotNum-1]){
trace("Save taken, confirming overwrite");
checkDialog.visible = true;
} else {
save(e);
}
}
public function cancelled(e){
showSaver();
}
public function overwrite(e){
save(currentEvent);
}
public function save(e){
puzzle = Engine.puzzle;
inv = Engine.inv;
player = Engine.player;
back = Engine.back;
options = Engine.options;
var saveNum:String = e.target.parent.name;
//Get the status of all the puzzles
allSaveData = new Object;
allSaveData.puzzleStatus = new Object;
allSaveData.puzzleStatus = puzzle.returnPuzzles();
//Get the inventory
allSaveData.allInv = new Array();
var allItems = inv.returnItems("all");
for (var i in allItems){
if(allItems[i].displayName){
//trace ("Saving " + allItems[i]);
allSaveData.allInv.push(allItems[i].displayName);
}
}
allSaveData.currentInv = new Array();
var currentItems = inv.returnItems(null);
for (i in currentItems){
allSaveData.currentInv.push([currentItems[i].displayName, currentItems[i].lookTag]);
}
//Get the player's current location
allSaveData.playerLoc = new Object;
allSaveData.playerLoc.x = player.x;
allSaveData.playerLoc.y = player.y;
allSaveData.playerLoc.scaleX = player.scaleX;
allSaveData.playerLoc.room = back.currentBack.name;
//Get the settings
allSaveData.optset = options.saveOptions();
// Get the date and time
var thisDate = new Date;
var dateString = thisDate.toLocaleString();
allSaveData.datetime = dateString;
var jsonSave = JSON.stringify(allSaveData);
trace("Saving: " + jsonSave);
//Save data in local memory
if(useShared){
shared = SharedObject.getLocal("myApp");
shared.data[saveNum] = jsonSave;
shared.flush();
}
if(useExternalFile){
// Here's where you can post the data to a server-side script and save it as an external .js file
// The trick would be to figure out how to distinguish one user from another
// Either by creating login functionality, or just identifying by IP or something like that
}
//Update save info
e.target.parent.savedetail.text = dateString;
showConfirm("saved", saveNum.substr(saveNum.length-1, 1));
dispatchEvent(new Event("closeThis"));
}
private function restore(e){
puzzle = Engine.puzzle;
inv = Engine.inv;
player = Engine.player;
back = Engine.back;
options = Engine.options;
var parentBtn = e.target.parent.name;
var slotNum = parentBtn.substr(parentBtn.length-1, 1);
var saveNum = "save" + slotNum;
trace(saveNum);
Engine.restoring = true;
allSaveData = null;
if(useExternalFile){
// Get data from external .js file
} else if (useShared){
// Get data from local memory
shared = SharedObject.getLocal("myApp");
if(shared.data[saveNum]){
trace("Restoring: " + shared.data[saveNum]);
allSaveData = new Object;
allSaveData = JSON.stringify(shared.data[saveNum]);
}
}
// Restore game data
if(allSaveData){
puzzle.restorePuzzles(allSaveData.puzzleStatus);
inv.restoreInv(allSaveData.currentInv, allSaveData.allInv);
options.restoreOptions(allSaveData.optset);
Engine.newBack = allSaveData.playerLoc.room;
addEventListener("repose", reposePlayer);
stageRef.dispatchEvent(new Event("changeBackground"));
showConfirm("restored", slotNum);
dispatchEvent(new Event("closeThis"));
} else {
trace ("No save data found");
Engine.restoring = false;
}
}
private function fakeScreenshot(bgName:String):Object{
// Flash's local memory is too small to contain real screenshots, so we can fake it by creating an instance of the background
var fakeShot:Object = new Object;
var bgRef = getDefinitionByName(bgName);
var bg = new bgRef;
// Remove all the interface junk from the background movieclip
for (var c = 0; c < bg.numChildren; ++c){
var thisChild = bg.getChildAt(c);
if (thisChild.name.search("_L") != -1 ||
thisChild.name.search("_U") != -1 ||
thisChild.name.search("_G") != -1 ||
thisChild.name.search("_T") != -1 ){
if (thisChild.usePoint)
thisChild.usePoint.visible = false;
}
if(thisChild.name.search("_O") != -1){
thisChild.usePoint.visible = false;
thisChild.depthSplit.visible = false;
thisChild.nodeUL.visible = false;
thisChild.nodeUR.visible = false;
thisChild.nodeLR.visible = false;
thisChild.nodeLL.visible = false;
}
if (thisChild.name.search("EXIT") != -1)
thisChild.visible = false;
if (thisChild.name.search("startPoint") != -1)
thisChild.visible = false;
}
bg.scaleX = .1;
bg.scaleY = .1;
bg.x = 10;
bg.y = 5;
var bgmask:MovieClip = new MovieClip;
bgmask.graphics.beginFill(0x000000, .5);
bgmask.graphics.drawRect(0, 0, 70, 45);
bgmask.graphics.endFill();
bgmask.x = bg.x;
bgmask.y = bg.y;
bg.mask = bgmask;
fakeShot.bg = bg;
fakeShot.bgmask = bgmask;
return fakeShot;
}
private function reposePlayer(e){
player = Engine.player;
player.x = allSaveData.playerLoc.x;
player.y = allSaveData.playerLoc.y;
player.scaleX = allSaveData.playerLoc.scaleX;
}
private function showConfirm(type:String, slot){
confirm = null;
confirm = new MovieClip;
confirm.graphics.beginFill(0x000000, .75);
confirm.graphics.drawRoundRect(0, 0, 200, 25, 10);
confirm.graphics.endFill();
stageRef.addChild(confirm);
var conftext:TextField = new TextField();
conftext.text = "Game " + type + " at slot " + slot;
var format1:TextFormat = new TextFormat();
format1.font = "Arial";
format1.size = 18;
format1.color = 0xFFFFFF;
conftext.setTextFormat(format1);
conftext.width = confirm.width - 5;
conftext.x = 5;
confirm.addChild(conftext);
confirm.addEventListener(Event.ENTER_FRAME, fadeConf);
}
private function fadeConf(e){
if (confirm.alpha > 0){
confirm.alpha -= .05;
} else {
confirm.removeEventListener(Event.ENTER_FRAME, fadeConf);
stageRef.removeChild(confirm);
}
}
private function closeSaver(e):void{
visible = false;
Engine.playerControl = true;
Engine.toolbar.dispatchEvent(new Event("closedWindow"));
}
}
}

Related

Dynamic Gallery to show download and progress bar in as3

My flash project has specific task to show the dynamic gallery items based on an XML list and there is a download option available for each gallery item.
For this I made a movieclip (imageTile) with a Thumbnail, Title, ProgressBar & ProgressText as shown below.
I have two classes named Main.as and FileRef.as
Main.as
var tileMap:Dictionary = new Dictionary();
public var tile:ImageTile;
addChild(wall);
wallWidth = wall.width;
wallHeight = wall.height;
var columns:Number;
var my_x:Number;
var my_y:Number;
var my_thumb_width:Number;
var my_thumb_height:Number;
var images:XMLList;
var total:Number;
var swipe:Number = 0;
var myXMLLoader:URLLoader = new URLLoader();
myXMLLoader.load(new URLRequest("gallery.xml"));
myXMLLoader.addEventListener(Event.COMPLETE, processXML);
function processXML(e:Event):void {
myXML = new XML(e.target.data);
images = myXML.IMAGE;
total = images.length();
myXMLLoader.removeEventListener(Event.COMPLETE, processXML);
myXMLLoader = null;
var loader:Loader;
for (var i:uint = 0; i < total; i++) {
tile = new ImageTile();
wall.addChild(tile);
tile.x = i % 3 * 400 + 180;
tile.y = Math.floor(i / 3) * 650 + 250;
var imageName:String = images[i].#FULL;
path = images[i].#Path;
var title:String = images[i].#Title;
**var caption:TextField = tile.getChildByName("caption_tf") as TextField;**
caption.text = title;
tile.addEventListener(MouseEvent.CLICK, onTileClick(path));
loader = new Loader();
loader.load(new URLRequest("images/" + imageName));
tileMap[loader] = tile;
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageLoad);
}
//trace(myXML);
}
var tileMap:Dictionary = new Dictionary();
function onImageLoad(e:Event):void {
var loader:Loader = e.target.loader;
var tile:ImageTile = tileMap[loader] as ImageTile;
var image:Bitmap = loader.content as Bitmap;
image.x = -100;
image.y = -100;
image.width=366;
image.height=418;
var textField:DisplayObject = tile.getChildByName("caption_tf");
var textFieldDepth:int = tile.getChildIndex(textField);
tile.addChildAt(image, textFieldDepth);
tileMap[loader] = null;
image.smoothing = true;
}
function onTileClick(url:String):Function {
return function(me:MouseEvent):void {
path = url;
var download:FileRef = new FileRef();
download.downloadFile(path);
}
FileRef.as code
public var mc_loaded : MovieClip = Main.gameInstance.tile.getChildByName("mc_loaded") as MovieClip,
mc_progress : MovieClip = Main.gameInstance.tile.getChildByName("mc_progress") as MovieClip,
txt_prog : TextField = Main.gameInstance.tile.getChildByName("txt_prog") as TextField;
public function downloadFile(url:String) : void
{
/// Download the gallery item codes using url
}
public function progressHandler( event : ProgressEvent ) : void
{
//mc_loaded.scaleX = (event.bytesLoaded / event.bytesTotal) ;
}
public function completeHandler( event : Event ) : void
{
//reset progress bar after download is finished
mc_loaded.scaleX = 0; // I want to use the imageTile element
txt_prog.text = "download finished";
}
public function OnZipComplete(evt:flash.events.Event):void
{
txt_prog.text = "download finished";
}
The download works fine, but I can not get the progress bar and progress text for every tile created.
I got the answer
Remove onTileClick(url:String) function from Main.as
Main.as
function processXML(e:Event):void {
//// Codes /////
//// Codes /////
var download:FileRefTut = new FileRefTut();
tile.addEventListener(MouseEvent.CLICK, download.downloadFile(path));
}
FileRef.as
public var txt_prog:TextField, mc_loaded:MovieClip;
public function downloadFile(url:String):Function {
return function(me:MouseEvent):void {
var tile:ImageTile = me.currentTarget as ImageTile;
txt_prog = tile.getChildByName("txt_prog") as TextField;
mc_loaded = tile.getChildByName("mc_loaded") as MovieClip;
}
public function progressHandler( event : ProgressEvent ) : void
{
mc_loaded.scaleX = (event.bytesLoaded / event.bytesTotal) ;
}
public function completeHandler( event : Event ) : void
{
//reset progress bar after download is finished
mc_loaded.scaleX = 0; // I want to use the imageTile element
txt_prog.text = "download finished";
}
public function OnZipComplete(evt:flash.events.Event):void
{
txt_prog.text = "download finished";
}

Added child stays invisible even though its container is visible

I have been struggling for a couple of days with an issue in Flash CS4. I am re-structuring an old game project into a Main class which handles the mainMenu, playGame, etc. functions. I have a ship added from the "game", which is added by Main.
The issue is "myShip" works as expected, except it's never visible. I've checked a lot of times, and both myShip and its containter (game) visible properties are always true. Alpha values are not the problem either, nor layers nor depth. Every other child I've added from "game" works just fine, but "myShip" refuses to be visible.
Any ideas as to why this could happen? I do not know how what to try next to solve the problem. Any help would be very appreciated. The code for the Main, Game and Ship class is below.
Thank you!
Code from the Main class:
public class Main extends Sprite {
public var mainMenuDisplay:MainMenuDisplay;
public var game:Game;
public var gameOverMenu:GameOverMenu;
public function Main() {
showMainMenu();
}
public function showMainMenu() {
mainMenuDisplay = new MainMenuDisplay(this);
gameOverMenu=remove_movie_clip(gameOverMenu);
addChild(mainMenuDisplay);
}
public function showGameOver() {
gameOverMenu = new GameOverMenu(this);
game=remove_movie_clip(game);
addChild(gameOverMenu);
}
public function playTheGame() {
game = new Game(this);
mainMenuDisplay = remove_movie_clip(mainMenuDisplay);
gameOverMenu=remove_movie_clip(gameOverMenu);
stage.addChild(game);
}
private function remove_movie_clip(clip:*) {
if (clip) {
removeChild(clip);
}
return null;
}
}
Code from the Game class:
package {
import flash.display.MovieClip;
import flash.display.Stage;
import flash.events.Event;
import flash.utils.Timer;
import flash.events.TimerEvent;
import com.coreyoneil.collision.CollisionList;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.events.MouseEvent;
import com.greensock.*;
import flash.display.Sprite;
import flash.display.SpreadMethod;
import flash.display.GradientType;
import flash.geom.Matrix;
import com.sounds.music.Music_mainMusic;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.display.DisplayObject;
public class Game extends MovieClip {
var mainClass:Main;
//Main menu
//var mainMenuDisplay:MainMenuDisplay = new MainMenuDisplay();
//static var inMenu:Boolean = true;
//
//Ship variables
static var myShip:Ship = new Ship();
var myDirectionBar:Direction_bar = new Direction_bar();
//
//Enemy variables
static var enemyShipTimer_1:Timer;
//
//PowerUp variables
static var powerUpTimer:Timer;
static var nuking:Boolean;
//
//Wall generation variables
static var wall_mov_speed:Number;
var randomize:Number = 1;
var wallArray:Array = new Array();
var index:int = 0;
//
//Wall collision variables (powered by CDK by Corey O'Neil)
var myWallCollisionList:CollisionList; // = new CollisionList(myShip);
var wall_collisions:Array = new Array();
//
//Score variables
static var score:Number;
static var scoreText:TextField = new TextField();
var scoreFormat = new TextFormat("LCD5x8H", 20, 0x0066FF, true);
var distance_score_counter:int;
//
//Health variables
static var healthMeter_1:HealthMeter = new HealthMeter();
//
//Game modes
//var levelSelectDisplay:LevelSelectDisplay = new LevelSelectDisplay();
//**NOTE: These are extremely important, because they are the functions, which in reality are attributes, that allow us to call,
//from an Event Listener, a function in which we have a parameter to pass. This way we call these variables instead of the
//function we are interested in, these will call it for us.
//var functionLevelSelect_1:Function = selectedLevel(1);
//var functionLevelSelect_2:Function = selectedLevel(2);
//var functionLevelSelect_3:Function = selectedLevel(3);
//var functionLevelSelect_4:Function = selectedLevel(4);
//var functionLevelSelect_5:Function = selectedLevel(5);
//The level composition (that's the numbers of the frame in the MC of the Walls, each number is a type. The last one stores all of them.
//var level_1_composition:Array = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1];
//var level_2_composition:Array = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1];
//var level_3_composition:Array = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1];
//var level_4_composition:Array = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1];
//var storyModeLevelCompositions:Array = new Array(level_1_composition, level_2_composition, level_3_composition, level_4_composition);
//
var levelPlaying:int = 0;
var wallPieceCount:int = 0;
//
//Pause variables
var pauseScreen:PauseScreen = new PauseScreen();
//This variables states whether we are in pause or not
static var isPause:Boolean = false;
//This other tells us if we can pause at the moment or not
static var isPauseable:Boolean = false;
//
//Game Over, new Game and Game menu variables
//static var gameOverMenu:GameOverMenu = new GameOverMenu();
static var inGameStopping:Boolean = false;
//
//Transition screen variables
var darkening:Boolean;
//NOTE: We do it this way because, when putting an Enter Frame event listener onto the function funcTransition,
//which has a pass variable, the variable changed all the time to true, giving us problems.
//Background graphics variables
var color1:uint = Math.floor(Math.random()*0xFFFFFF + 1);
var color2:uint = Math.floor(Math.random()*0xFFFFFF + 1);
var colors:Object = {left:color1, right:color2};
var newColor1:uint = Math.floor(Math.random()*0xFFFFFF + 1);
var newColor2:uint = Math.floor(Math.random()*0xFFFFFF + 1);
var newColors:Object = {left:newColor1, right:newColor2};
var mySprite:Sprite = new Sprite();
//
//Music variables
var myMainMusic:Music_mainMusic = new Music_mainMusic();
//
//Credits variables
//var myCredits:Credits = new Credits();
//var myVersion:VersionDisplay = new VersionDisplay();
//
//Other variables
//var initThingy:Boolean;
var initTransition:Boolean = true;
var allPurposeCounter:int = 0;
var myTransitionScreen:TransitionScreen = new TransitionScreen();
//
//New necessary variables
//
public function Game(passedClass:Main) {
mainClass = passedClass;
if (stage) {
init(null);
}else{
this.addEventListener(Event.ADDED_TO_STAGE, init);
}
}
public function init(e:Event) {
this.removeEventListener(Event.ADDED_TO_STAGE, init);
this.parent.addChild(this);
//Necessary initial booting:
mySprite.x = 0;
mySprite.y = 0;
stage.addChildAt(mySprite, 1);
drawGradient();
animateBackground();
//////////////////////////////////////////////////////
/*mainMenuDisplay.x = 400 - mainMenuDisplay.width/2;
mainMenuDisplay.y = 240 - mainMenuDisplay.height/2;
stage.addChild(mainMenuDisplay);*/
//////////////////////////////////////////////////////
//////////////////////////////////////////////////////
/*levelSelectDisplay.x = 400 - levelSelectDisplay.width/2;
levelSelectDisplay.y = 240 - levelSelectDisplay.height/2;
levelSelectDisplay.visible = false;
stage.addChild(levelSelectDisplay);*/
//////////////////////////////////////////////////////
//Transitions
myTransitionScreen.visible = false;
stage.addChild(myTransitionScreen);
//
//////////////////////////////////////////////////////
//myCredits.x = 20;
//myCredits.y = 438;
//stage.addChild(myCredits);
//myVersion.x = 710;
//myVersion.y = 438;
//stage.addChild(myVersion);
//////////////////////////////////////////////////////
//myMainMusic.play(0,99999);
initGame(null);
//mainMenuIdleState();
//
}
//////////////////////////////////////////////////////
/*function mainMenuIdleState(){
stage.addChild(mainMenuDisplay);
stage.addChild(levelSelectDisplay);
inMenu = true;
mainMenuDisplay.visible = true;
mainMenuDisplay.mainMenuPlayStoryButton_instance.addEventListener(MouseEvent.MOUSE_DOWN, level_select);
mainMenuDisplay.mainMenuPlayEndlessButton_instance.addEventListener(MouseEvent.MOUSE_DOWN, endless_mode_selected);
}*/
//////////////////////////////////////////////////////
//////////////////////////////////////////////////////
/*function endless_mode_selected(e:Event){
levelPlaying = 0;
initGame(null);
}*/
//////////////////////////////////////////////////////
//////////////////////////////////////////////////////
/*function level_select(e:Event){
mainMenuDisplay.visible = false;
levelSelectDisplay.visible = true;
levelSelectDisplay.levelSelectButton1_instance.addEventListener(MouseEvent.MOUSE_DOWN, functionLevelSelect_1);
levelSelectDisplay.levelSelectButton2_instance.addEventListener(MouseEvent.MOUSE_DOWN, functionLevelSelect_2);
levelSelectDisplay.levelSelectButton3_instance.addEventListener(MouseEvent.MOUSE_DOWN, functionLevelSelect_3);
levelSelectDisplay.levelSelectButton4_instance.addEventListener(MouseEvent.MOUSE_DOWN, functionLevelSelect_4);
levelSelectDisplay.levelSelectButtonBack_instance.addEventListener(MouseEvent.MOUSE_DOWN, functionLevelSelect_5);
}
function selectedLevel(level:int):Function {
switch (level){
case 1:
return function(e:MouseEvent):void {
//trace("1 clicked");
levelPlaying = 1;
levelSelectDisplay.visible = false;
initGame(null);
}
break;
case 2:
return function(e:MouseEvent):void {
//trace("2 clicked");
levelPlaying = 2;
levelSelectDisplay.visible = false;
initGame(null);
}
break;
case 3:
return function(e:MouseEvent):void {
//trace("3 clicked");
levelPlaying = 3;
levelSelectDisplay.visible = false;
initGame(null);
}
break;
case 4:
return function(e:MouseEvent):void {
//trace("4 clicked");
levelPlaying = 4;
levelSelectDisplay.visible = false;
initGame(null);
}
break;
default:
return function(e:MouseEvent):void {
//trace("back clicked");
levelPlaying = 0;
levelSelectDisplay.visible = false;
mainMenuDisplay.visible = true;
levelSelectDisplay.levelSelectButton1_instance.removeEventListener(MouseEvent.MOUSE_DOWN, functionLevelSelect_1);
levelSelectDisplay.levelSelectButton2_instance.removeEventListener(MouseEvent.MOUSE_DOWN, functionLevelSelect_2);
levelSelectDisplay.levelSelectButton3_instance.removeEventListener(MouseEvent.MOUSE_DOWN, functionLevelSelect_3);
levelSelectDisplay.levelSelectButton4_instance.removeEventListener(MouseEvent.MOUSE_DOWN, functionLevelSelect_4);
levelSelectDisplay.levelSelectButtonBack_instance.removeEventListener(MouseEvent.MOUSE_DOWN, functionLevelSelect_5);
}
break;
}
}*/
//////////////////////////////////////////////////////
function initGame(e:Event):void{
//This has so many redundancies, when everything is done, START CLEANING THIS!
//////////////////////////////////////////////////////
//Main menu
//mainMenuDisplay.visible = false;
//inMenu = false; THIS GOES AT THE END TO PREVENT PROBLEMS
//directNewGame tells us if we come from the newGame function (and thus we do not go through the mainMenuIdleState
//function and this instances have not been placed on stage) or not. If we come from the main menu, we DO need to
//remove them.
//
trace(myShip);
//Ship
myShip.x = -10; //Before there were numbers to implement stage.stageWidth/2;
myShip.y = 200; //Before there were numbers to implement stage.stageHeight/2;
myShip.visible = true;
//mainClass.addChild(myShip);
this.addChild(myShip);
//We make sure the ship doesn't enter to stage with 0 health
//(problems of working with only one instance of ship due to the static var references)
Ship.health = 100;
//Check "NOTE" below
myShip.alpha = 0.35;
myShip.visible = true;
//
trace(myShip.visible);
//Direction bar
myDirectionBar.x = stage.stageWidth/2;
myDirectionBar.y = stage.stageHeight/2;
this.addChild(myDirectionBar);
//
//Timers (enemies)
enemyShipTimer_1 = new Timer(1000)
enemyShipTimer_1.addEventListener(TimerEvent.TIMER, spawn_enemies);
enemyShipTimer_1.start();
//
//Timer (powerUps)
powerUpTimer = new Timer(10000);
powerUpTimer.addEventListener(TimerEvent.TIMER, spawn_powerUp);
powerUpTimer.start();
//
//PowerUps (other)
nuking = false;
//
myWallCollisionList = new CollisionList(myShip);
//Initial movement speed of the walls
wall_mov_speed = 8;
//Calling to the generating/adequating wallArray function
adequateArrayOfWalls(true);
wallArray[0].gotoAndStop(1);
wallArray[1].gotoAndStop(1);
myWallCollisionList.addItem(wallArray[0].theActualWall);
myWallCollisionList.addItem(wallArray[1].theActualWall);
//Collision managements
wall_collisions = 0 as Array;
//NOTE: Here we limit the alpha value to consider for collision just to make sure the game doesn't start with you killed, and that you are "invincible"
//for some time
myWallCollisionList.alphaThreshold = 0.95;
//
//Adding score format and text
scoreText.defaultTextFormat = scoreFormat;
scoreText.x = 700;
scoreText.y = 10;
score = 0;
scoreText.text = String(score);
stage.addChild(scoreText);
distance_score_counter = 0;
scoreText.visible = true;
//
//Adding health meter
healthMeter_1 = new HealthMeter();
healthMeter_1.x = 10;
healthMeter_1.y = 10;
stage.addChild(healthMeter_1);
//
//Adding the Pause screen & other pause variables
pauseScreen.x = 400 - pauseScreen.width/2;
pauseScreen.y = 240 - pauseScreen.height/2;
pauseScreen.visible = false;
stage.addChild(pauseScreen);
isPauseable = true;
//Adding a key managing event (for pausing, menu, etc.)
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyManaging);
//
/*//Adding a Game Over Menu
gameOverMenu = new GameOverMenu();
gameOverMenu.x = 400 - gameOverMenu.width/2;
gameOverMenu.y = 240 - gameOverMenu.height/2;
gameOverMenu.visible = false;
stage.addChild(gameOverMenu);
gameOverMenu.playAgainButton_instance.addEventListener(MouseEvent.MOUSE_DOWN, newGame);
gameOverMenu.backToMenuButton_instance.addEventListener(MouseEvent.MOUSE_DOWN, backToTheMenu);
//*/
//Shield
//
//Event listeners
addEventListener(Event.ENTER_FRAME, update_game);
//
//////////////////////////////////////////////////////
/*//Credits
myCredits.visible = false;
myVersion.visible = false;
//
initThingy = true;
inMenu = false;*/
//////////////////////////////////////////////////////
}
function update_game(e:Event){
myShip.visible = true;
//Look the adequate array function for more info. We are just moving the two pieces of the array on stage
wallArray[(index - 1)].x -= wall_mov_speed;
wallArray[index].x -= wall_mov_speed;
if(wallArray[index].x < 0){
spawn_wall_piece();
}
//
if(index == 5){
//We call this function for cleaning
adequateArrayOfWalls(false);
}
if(wall_mov_speed < 20){
wall_mov_speed += 0.003;
}
wall_collisions = myWallCollisionList.checkCollisions();
if(wall_collisions.length > 0){
trace("hit!");
if(myShip.visible == true){
//We only kill the ship if it's visible, if not, it means it is already dead
Ship.receiveDamage(Ship.max_health);
}
wall_collisions = 0 as Array;
}
if(distance_score_counter >= 10){
distance_score_counter = 0;
updateScore(1);
}
distance_score_counter++;
//NOTE2: We use this nuke variable in order not to make the "nuke()" function static, type in which we couldn't handle the stage property
//And we also make this variable false here so as to eliminate not only a single enemy but all on stage
Enemy1.enemies_1Nuked = false;
if(nuking == true){
Enemy1.enemies_1Nuked = true;
nuking = false;
}
//We put these all the time at the front so we can see them and the walls don't overpass them
scoreText.parent.setChildIndex(scoreText, scoreText.parent.numChildren - 1);
healthMeter_1.parent.setChildIndex(healthMeter_1, healthMeter_1.parent.numChildren - 1);
pauseScreen.parent.setChildIndex(pauseScreen, pauseScreen.parent.numChildren -1);
//gameOverMenu.parent.setChildIndex(gameOverMenu, gameOverMenu.parent.numChildren - 1);
var n:uint = stage.numChildren;
for(var i=0; i < n; i++){
if(stage.getChildAt(i) is Enemy1){
var anEnemy1:Enemy1 = Enemy1(stage.getChildAt(i));
anEnemy1.parent.setChildIndex(anEnemy1, anEnemy1.parent.numChildren -1);
}
else if(stage.getChildAt(i) is PowerUp){
var aPowerUp:PowerUp = PowerUp(stage.getChildAt(i));
aPowerUp.parent.setChildIndex(aPowerUp, aPowerUp.parent.numChildren -1);
}
}
//Done like this due to the impossibility of calling a function inside an static one (in this case, gameOver)
if(inGameStopping == true){
funcEasing();
}
//Probably not necessary later
//////////////////////////////////////////////////////
/*if(initThingy == true){
stage.focus = stage;
initThingy = false;
}*/
//////////////////////////////////////////////////////
}
function spawn_enemies(e:Event){
var myEnemy1:Enemy1 = new Enemy1();
stage.addChild(myEnemy1);
}
function spawn_wall_piece(){
index++;
wallArray[index].x = (wallArray[index - 1].x + wallArray[index - 1].width);
wallArray[index].y = 0;
stage.addChild(wallArray[index]);
myWallCollisionList.addItem(wallArray[index].theActualWall);
myWallCollisionList.removeItem(wallArray[index - 2].theActualWall);
stage.removeChild(wallArray[index - 2]);
}
function adequateArrayOfWalls(init:Boolean):void{
//This only executes if we are initialitizing the array
if(init == true){
for(index = 0; index < 10; index++){
var aWall:Walls = new Walls();
//We check if we got special blocks next (e.g. "ramp caves"). Then we only allow a certain type of blocks to come.
//If no special block is detected, then we just randomize the next one, except for those that are not allowed to
//show up unless a previous special one appeared.
if(randomize == 9 || randomize == 15){
randomize = 15 + Math.floor(Math.random()*1 + 1);
}else{
randomize = Math.floor(Math.random()*14 + 1);
}
aWall.gotoAndStop(randomize);
//TheActualWall is the raw shape of the wall, where the ship collides, and it is what we push into collisionList,
//but not into the wallArray which includes the Walls (comprised by graphics and actual walls)
aWall.theActualWall.gotoAndStop(randomize);
wallArray.push(aWall);
}
wallArray[0].gotoAndStop(1);
wallArray[0].theActualWall.gotoAndStop(1);
stage.addChild(wallArray[0]);
wallArray[1].x = 800;
wallArray[1].y = 0;
stage.addChild(wallArray[1]);
//if not, then we are just cleaning it and rearranging it so it doesn't grow bigger and bigger
}else{
for(var a:Number = 0; a < index - 1; a++){
wallArray.splice(0,1);
}
for(a = index - 1; a < (10-2); a++){
var aWall2:Walls = new Walls();
if(randomize == 9 || randomize == 15){
randomize = 15 + Math.floor(Math.random()*1 + 1);
}else{
randomize = Math.floor(Math.random()*14 + 1);
}
aWall2.gotoAndStop(randomize);
aWall2.theActualWall.gotoAndStop(randomize);
wallArray.push(aWall2);
}
}
//Then, either way, we tell index to be 1 since the reference in the function is [index - 1] and [index], so it starts with [0] and [1]
index = 1;
}
static function updateScore(points:Number){
score += points;
scoreText.text = score.toString();
}
static function resetScore(){
score = 0;
scoreText.text = score.toString();
}
function spawn_powerUp(e:Event){
var pU:PowerUp = new PowerUp();
stage.addChild(pU);
}
static function gameOver(){
wall_mov_speed = 8;
//gameOverMenu.end_game_score_display.text = score.toString();
//gameOverMenu.visible = true;
scoreText.visible = false;
enemyShipTimer_1.stop();
powerUpTimer.stop();
inGameStopping = true; //In game stopping only influentiates in the easing speed effect
isPauseable = false;
}
function funcEasing(){
if(wall_mov_speed >= 0.1){
wall_mov_speed /= 1.07;
}else{
wall_mov_speed = 0;
removeEventListener(Event.ENTER_FRAME, update_game);
initTransition = true;
darkening = true; //See notes on variable declaration.
funcTransition(null);
}
}
function funcTransition(e:Event){
if(initTransition == true){
myTransitionScreen.init(darkening);
myTransitionScreen.parent.setChildIndex(myTransitionScreen, stage.numChildren - 1);
myTransitionScreen.parent.addEventListener(Event.ENTER_FRAME, funcTransition);
initTransition = false;
allPurposeCounter = 0;
}
if((darkening == true && myTransitionScreen.alpha == 1) || (darkening == false && myTransitionScreen.alpha == 0)){
trace("fsdfa");
allPurposeCounter++;
trace(allPurposeCounter);
if(allPurposeCounter >= 20){
myTransitionScreen.parent.removeEventListener(Event.ENTER_FRAME, funcTransition);
initTransition = true;
allPurposeCounter = 0;
if(darkening == true){ //This means if we are now with a black screen coming from the game, which is when we will end our game process
endGameProcess();
}
}
}
}
function endGameProcess(){
mainClass.showGameOver();
}
function newGame(e:Event){
darkening = true; //See notes on variable declaration.
initTransition = true;
funcTransition(null);
}
//Check To-Do List below
function funcPause(pMode:String){
if(pMode == "pausing"){
pauseScreen.visible = true;
removeEventListener(Event.ENTER_FRAME, update_game);
myShip.thePause("pausing");
//Check and stop the childs on stage (emitted by stage, so particles don't count)
var n:uint = stage.numChildren;
for(var i=0; i < n; i++){
if(stage.getChildAt(i) is Enemy1){
var anEnemy1:Enemy1 = Enemy1(stage.getChildAt(i));
anEnemy1.thePause("pausing");
}
else if(stage.getChildAt(i) is Trail){
var aTrailUnit:Trail = Trail(stage.getChildAt(i));
aTrailUnit.thePause("pausing");
}
else if(stage.getChildAt(i) is PowerUp){
var aPowerUp:PowerUp = PowerUp(stage.getChildAt(i));
aPowerUp.thePause("pausing");
}
}
enemyShipTimer_1.stop();
powerUpTimer.stop();
isPause = true;
isPauseable = false;
}else if(pMode == "unpausing"){
pauseScreen.visible = false;
addEventListener(Event.ENTER_FRAME, update_game);
myShip.thePause("unpausing");
//Check and re-run the childs on stage (emitted by stage, so particles don't count)
var m:uint = stage.numChildren;
for(var j=0; j < m; j++){
if(stage.getChildAt(j) is Enemy1){
var anotherEnemy1:Enemy1 = Enemy1(stage.getChildAt(j));
anotherEnemy1.thePause("unpausing");
}
else if(stage.getChildAt(j) is Trail){
var anotherTrailUnit:Trail = Trail(stage.getChildAt(j));
anotherTrailUnit.thePause("unpausing");
}
else if(stage.getChildAt(j) is PowerUp){
var anotherPowerUp:PowerUp = PowerUp(stage.getChildAt(j));
anotherPowerUp.thePause("unpausing");
}
}
enemyShipTimer_1.start();
powerUpTimer.start();
isPause = false;
isPauseable = true;
}
}
//Key pressing management
function keyManaging(e:KeyboardEvent){
var key:uint = e.keyCode;
trace("algo");
switch (key){
case Keyboard.P:
if(isPause == false && isPauseable == true){
funcPause("pausing");
}else if (isPause == true){
funcPause("unpausing");
}
break;
case Keyboard.M:
//go back to menu: still to complete
//Has to be only possible to do while in the pause menu
trace("going back to menu");
//
break;
}
}
//
//Background color management
function drawGradient():void {
var m:Matrix = new Matrix();
m.createGradientBox(805, 485, 0, 0, 0);
mySprite.graphics.clear(); // here we clean it
mySprite.graphics.beginGradientFill(GradientType.LINEAR, [colors.left, colors.right], [1, 1], [0x00, 0xFF], m, SpreadMethod.REFLECT);
mySprite.graphics.drawRoundRect(0,0,805,485, 0);
stage.setChildIndex(mySprite, 1);
}
function animateBackground(){
TweenMax.to(colors, 3, {hexColors:{left:newColor1, right:newColor2}, onUpdate:drawGradient, onComplete:reRandomize});
}
function reRandomize(){
color1 = newColor1;
color2 = newColor2;
newColor1 = Math.floor(Math.random()*0xFFFFFF + 1);
newColor2 = Math.floor(Math.random()*0xFFFFFF + 1);
animateBackground();
}
}
}
Code from Ship:
public class Ship extends MovieClip {
public function Ship() {
if (stage) {
init(null);
}else{
this.addEventListener(Event.ADDED_TO_STAGE, init);
}
}
public function init(e:Event) {
this.removeEventListener(Event.ADDED_TO_STAGE, init);
this.addEventListener(Event.ENTER_FRAME, update_ship);
}
public function update_ship(e:Event){
x_vel = Direction_bar.dX*power;
y_vel = Direction_bar.dY*power;
this.x += x_vel;
this.y += y_vel;
if((10 < Math.abs(Direction_bar.dX) || 10 < Math.abs(Direction_bar.dY)) || ((0.9 < Math.abs(x_vel)||(0.9 < Math.abs(y_vel))))){
this.rotation = Direction_bar.point_direction;
}
rotation_now = this.rotation;
if(myShield != null){
if(myShield.visible == true){
myShield.alpha -= 0.0005;
if(myShield.alpha == 0){
myShield.visible = false;
myShield.alpha = 1;
}
}
}
}
Some basics that you have to know in order to understand what's going on.
It's a very common mistake to add things to stage.
Here's what the documentation of addChild says
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/DisplayObjectContainer.html#addChild%28%29
objects should not be added to the Stage, directly, at all
I guess people add DispalyObjects to stage because they think it is
"the stage" that they see and interact with in the Flash authoring
environment. But it's not. stage.addChild() is not the same
thing as dragging a symbol from the library onto the screen. What by
default represents the main time line is the root property.
However, if you add anything to stage directly, its root property and its stage property both reference the same object,
which is regularly only referenced by stage. stage is some
container that your .swf is added to when running in the flash
player.
The documentation of addChildAt says this about the index:
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/DisplayObjectContainer.html#addChildAt%28%29
The child is added at the index position specified. An index of 0 represents the back (bottom) of the display list for this
DisplayObjectContainer object.
Applying these basics, the following happens:
FlashPlayer creates the stage object, instantiates your main class
and adds that instance to stage, it is the child with the index 0.
Among other things, stage.addChildAt(mySprite, 1); is executed,
adding mySprite as a second child to stage. With index of 1 it is
rendered in front of the object that is at index 0, which happens to
be the instance of your main class AKA your .swf file. I hope that
anything being rendered "outside the .swf file" illustrates well
enough why adding things to stage is not recommended.
Later, this.addChild(myShip); happens. (Which is actually the
proper way to do it; no need to use this here:addChild(myShip);
is all you need.) and adds the ship to the display list. Assuming all
of its parents are added to the display list as well, it will be
displayed.
But you still cannot see it, because you added mySprite in front of
the instance of your main class and filled it with a content in
drawGradient() which covers up everything else.
In all honesty, the best solution would be to start over from scratch.
Working with this code will not help you in any way. Even working yourself through it and making it work somehow will not make you understand anything better (except for how not to do things). It seems like the only motivation to modify this code to use classes was for the sake of doing it. Forcing such old code into the object oriented paradigm will not work very well. The benefits of oop will not be apparent, making this experience even more frustrating.
Last but not least, do not roll your own transition code. There are many libraries that do this (including flash's own Tween class http://www.republicofcode.com/tutorials/flash/as3tweenclass/ or the popular tweenlite http://greensock.com/tweenlite)
You could try adding an ADDED_TO_STAGE event. See this excellent explanation from the master of game programming
Understanding ADDED_TO_STAGE Event

#2007: Parameter format must be non-null?

i have a problem with my as3 code,
i try to link a movie clip to different scene (quiz scene).
so,i have 2 quiz in total, first quiz is using external script (as)
And the second quiz have the same script like first quiz, but i placed the action script inside fla. and it had different xml.
but then this error message appeared:
TypeError: Error #2007: Parameter format must be non-null.
at flash.text::TextField/set defaultTextFormat()
at hiragana/createText()[E:\flash\!!!! FLASH JADI\PAK ASHAR FIX\hiragana.as:80]
at hiragana/kuisdua()[hiragana::frame209:48]
at hiragana/frame209()[hiragana::frame209:249]
this is the code:
// creates a text field
public function createText(text:String, tf:TextFormat, s:Sprite, x,y: Number, width:Number): TextField {
var tField:TextField = new TextField();
tField.x = x;
tField.y = y;
tField.width = width;
tField.defaultTextFormat = tf; //looks like this is the source of problem (-.-)
tField.selectable = false;
tField.multiline = true;
tField.wordWrap = true;
if (tf.align == "left") {
tField.autoSize = TextFieldAutoSize.LEFT;
} else {
tField.autoSize = TextFieldAutoSize.CENTER;
}
tField.text = text;
s.addChild(tField);
return tField;
}
and this is the ettire code
import flash.display.*;
import flash.text.*;
import flash.events.*;
import flash.net.URLLoader;
import flash.net.URLRequest;
///*public class*/ kuisduaa extends MovieClip {
// question data
/*private*/ var dataXML2:XML;
// text formats
/*private*/ var questionFormat2:TextFormat;
/*private*/ var answerFormat2:TextFormat;
/*private*/ var scoreFormat2:TextFormat;
// text fields
/*private*/ var messageField2:TextField;
/*private*/ var questionField2:TextField;
/*private*/ var scoreField2:TextField;
// sprites and objects
/*private*/ var gameSprite2:Sprite;
/*private*/ var questionSprite2:Sprite;
/*private*/ var answerSprites2:Sprite;
/*private*/ var gameButton2:GameButton;
// game state variables
/*private*/ var questionNum2:int;
/*private*/ var correctAnswer2:String;
/*private*/ var numQuestionsAsked2:int;
/*private*/ var numCorrect2:int;
/*private*/ var answers2:Array;
/*public*/ function kuisdua() {
// create game sprite
gameSprite2 = new Sprite();
addChild(gameSprite2);
// set text formats
questionFormat2 = new TextFormat("Arial",80,0xffffff,true,false,false,null,null,"center");
answerFormat2 = new TextFormat("Arial",50,0xffffff,true,false,false,null,null,"left");
scoreFormat2 = new TextFormat("Arial",30,0xffffff,true,false,false,null,null,"center");
// create score field and starting message text
scoreField2 = createText("",scoreFormat,gameSprite,-30,550,550);
messageField2 = createText("Loading Questions...",questionFormat,gameSprite,0,50,550);
// set up game state and load questions
questionNum2 = 0;
numQuestionsAsked2 = 0;
numCorrect2 = 0;
showGameScore2();
xmlImport2();
}
// start loading of questions
/*public*/ function xmlImport2() {
var xmlURL:URLRequest = new URLRequest("kuis2.xml");
var xmlLoader:URLLoader = new URLLoader(xmlURL);
xmlLoader.addEventListener(Event.COMPLETE, xmlLoaded);
}
// questions loaded
/*public*/ function xmlLoaded2(event:Event) {
dataXML = XML(event.target.data);
gameSprite.removeChild(messageField);
messageField = createText("Tap Untuk Memulai",scoreFormat,gameSprite,-10,250,500);
showGameButton("mulai");
}
// creates a text field
/*public*/ function createText2(text:String, tf:TextFormat, s:Sprite, x,y: Number, width:Number): TextField {
var tField2:TextField = new TextField();
tField2.x = x;
tField2.y = y;
tField2.width = width;
tField2.defaultTextFormat = tf;
tField2.selectable = false;
tField2.multiline = true;
tField2.wordWrap = true;
if (tf.align == "left") {
tField2.autoSize = TextFieldAutoSize.LEFT;
} else {
tField2.autoSize = TextFieldAutoSize.CENTER;
}
tField2.text = text;
s.addChild(tField2);
return tField2;
}
// updates the score
/*public*/ function showGameScore2() {
scoreField2.text = "Soal: "+numQuestionsAsked2+" Benar: "+numCorrect2;
}
// ask player if they are ready for next question
/*public*/ function showGameButton2(buttonLabel:String) {
gameButton = new GameButton();
gameButton.label.text = buttonLabel;
gameButton.x = 240;
gameButton.y = 480;
gameSprite2.addChild(gameButton);
gameButton.addEventListener(MouseEvent.CLICK,pressedGameButton2);
}
// player is ready
/*public*/ function pressedGameButton2(event:MouseEvent) {
// clean up question
if (questionSprite2 != null) {
gameSprite2.removeChild(questionSprite2);
}
// remove button and message
gameSprite2.removeChild(gameButton);
gameSprite2.removeChild(messageField2);
// ask the next question
if (questionNum >= dataXML.child("*").length()) {
gotoAndStop(6);
} else {
askQuestion2();
}
}
// set up the question
/*public*/ function askQuestion2() {
// prepare new question sprite
questionSprite2 = new Sprite();
gameSprite2.addChild(questionSprite2);
// create text field for question
var question2:String = dataXML.item[questionNum].question2;
if (dataXML.item[questionNum].question.#type == "text") {
questionField2 = createText(question2,questionFormat2,questionSprite2,50,150,300);
} else {
var questionLoader2:Loader = new Loader();
var questionRequest2:URLRequest = new URLRequest("triviaimages/"+question2);
questionLoader2.load(questionRequest2);
questionLoader2.y = 150;
questionLoader2.x = 180;
questionSprite2.addChild(questionLoader2);
}
// create sprite for answers, get correct answer and shuffle all
correctAnswer2 = dataXML.item[questionNum2].answers.answer[0];
answers2 = shuffleAnswers(dataXML.item[questionNum2].answers);
// put each answer into a new sprite with a icon
answerSprites2 = new Sprite();
var xpos:int = 0;
var ypos:int = 0;
for(var i:int=0;i<answers2.length;i++) {
var answerSprite2:Sprite = new Sprite();
if (answers2[i].type == "text") {
var answerField2:TextField = createText(answers2[i].value,answerFormat2,answerSprite2,30,-35,200);
} else {
var answerLoader2:Loader = new Loader();
var answerRequest2:URLRequest = new URLRequest("triviaimages/"+answers2[i].value);
answerLoader2.load(answerRequest2);
answerLoader2.y = -22;
answerLoader2.x = 25;
answerSprite2.addChild(answerLoader2);
}
var letter:String = String.fromCharCode(65+i); // A-D
var circle:Circle = new Circle(); // from Library
circle.letter.text = letter;
circle.answer = answers[i].value;
answerSprite2.x = 100+xpos*250;
answerSprite2.y = 350+ypos*100;
xpos++
if (xpos > 1) {
xpos = 0;
ypos += 1;
}
answerSprite2.addChild(circle);
answerSprite2.addEventListener(MouseEvent.CLICK,clickAnswer); // make it a button
// set a larger click area
answerSprite2.graphics.beginFill(0x000000,0);
answerSprite2.graphics.drawRect(-50, 0, 200, 80);
answerSprites2.addChild(answerSprite2);
}
questionSprite2.addChild(answerSprites2);
}
// take all the answers and shuffle them into an array
/*public*/ function shuffleAnswers2(answers:XMLList) {
var shuffledAnswers2:Array = new Array();
while (answers2.child("*").length() > 0) {
var r:int = Math.floor(Math.random()*answers.child("*").length());
shuffledAnswers2.push({type: answers2.answer[r].#type, value: answers2.answer[r]});
delete answers2.answer[r];
}
return shuffledAnswers2;
}
// player selects an answer
/*public*/ function clickAnswer2(event:MouseEvent) {
// get selected answer text, and compare
var selectedAnswer2 = event.currentTarget.getChildAt(1).answer;
if (selectedAnswer2 == correctAnswer2) {
numCorrect++;
messageField2 = createText("Hai, kamu benar ! ",scoreFormat2,gameSprite2,-30,280,550);
} else {
messageField2 = createText("Iie, Jawabanmu Salah, yang benar adalah:",scoreFormat2,gameSprite2,53,280,370);
}
finishQuestion();
}
/*public*/ function finishQuestion2() {
// remove all but the correct answer
for(var i:int=0;i<4;i++) {
answerSprites2.getChildAt(i).removeEventListener(MouseEvent.CLICK,clickAnswer);
if (answers2[i].value != correctAnswer2) {
answerSprites2.getChildAt(i).visible = false;
} else {
answerSprites2.getChildAt(i).x = 200;
answerSprites2.getChildAt(i).y = 400;
}
}
// next question
questionNum2++;
numQuestionsAsked2++;
showGameScore2();
showGameButton2("Lanjutkan");
}
// clean up sprites
/*public*/ function CleanUp2() {
removeChild(gameSprite);
gameSprite2 = null;
questionSprite2 = null;
answerSprites2 = null;
dataXML2 = null;
}
the first quiz played perfectly with that code,
and i have no clue why this error appeared,
i'm beginner in as3 so i'm still lack in many ways,
can anyone help me,?
i really apreciate it.. :)
Are you sure that you're parsing a non-null TextFormat instance as the second argument of createText()?
The error means that you're supplying a null value for tf:TextFormat.
Unless I am missing something, your issue is here:
messageField2 = createText("Loading Questions...",questionFormat,gameSprite,0,50,550);
&
messageField = createText("Tap Untuk Memulai",scoreFormat,gameSprite,-10,250,500);
I see questionFormat2 and scoreFormat2 are instantiated, but I never see a questionFormat or scoreFormat. Your error says that the defaultTextFormat (or TextFormat supplied using setTextFormat(), for future reference), cannot be null. Null means that the object has not been instantiated/created yet. questionFormat and scoreFormat are never instantiated. Hell, their variables do not even exist. If you were using a proper development IDE (FlashBuilder, FlashDevelop, etc), you'd never be able to compile this code.
Switch those two lines to use the correct formats and you should be fine.

event listener won't work second time around AS3

I'm stuck! If I need to go into more detail please just leave a comment and I will elaborate.
I've tried to create some code that dynamically adds linked objects to the stage and then removes then when dropped in the correct area, which in turn creates the next and so on. The information is in an array and it cycles through until the game is complete and the targetScore is met. If a timer runs out the games stops, and the win() or lose() functions are called and a retry button is displayed. This works fine until the game has stopped and I try to restart the program using the retry() function. The retry() function attempts to reset everything as it was when the program started, by creating the baseball object again and then letting the releaseToDrop() repeat everything as it did the first time. Depending on where I have stopped, when I get around to the same place a second time, the clicktoDrag1() function fails to pick up the current object. It could be on any 1 of the 8 objects that are created dynamically from the library. When the stage hears the listener on *mouse_up* I can click and drag the object but then it kind of falls apart as it goes slightly out of synch with the arrays (which were reset in the retry() function), the target which it's dropped on doesn't recognise it even though all the trace statement read as they should. I know this is a lot to look though, and I'm not sure if this can be solved via the forum, any general tips would be appreciated though.
I normally keep the code simple, but I want to advance and make the code write itself, which seems to work until things get too complicated for me.
I've never posted for help before, but I've given this everything, if it can't be fixed I'll have to start again and simplify.
Thanks in advance if anyone takes the time to look at this, I would be humbled - and would love to use this forum more to become a better coder.
It's all on the timeline, here's the code.
import flash.display.MovieClip;
import flash.ui.Mouse;
var startPosX = 450;
var startPosY = 400;
//setup first clip
var baseball:MovieClip = new Baseball();
baseball.name = "baseball";
addChild(baseball);
baseball.buttonMode = true;
baseball.x = startPosX;
baseball.y = startPosY;
activity_txt.text = "Swinging a baseball bat";
//setup small clips
baseballSmall.visible = false;
golfSmall.visible = false;
swimSmall.visible = false;
boxingSmall.visible = false;
tennisSmall.visible = false;
dartsSmall.visible = false;
powerSmall.visible = false;
marathonSmall.visible = false;
theEnd.visible = false;
retry_btn.visible = false;
fast1.visible = false;
fast2.visible = false;
fast3.visible = false;
fast4.visible = false;
slow1.visible = false;
slow2.visible = false;
slow3.visible = false;
slow4.visible = false;
//setup vars
var counter:int = 0;
var sportCounter:int = 0;
var startingLife:int = 15;
var playerLife = startingLife;
var lifeBoost:int = 3;
var targetScore:int = 8;
var countdownTimer:Timer = new Timer(500,0);
var questionTimer:Timer = new Timer(250,2);
var score:int = 0;
var smallArray = new Array("baseballSmall","golfSmall", "swimSmall", "boxingSmall","tennisSmall", "dartsSmall", "powerSmall", "marathonSmall");
var sportArray = new Array("baseball","Golf", "Swimming", "Boxing", "Tennis", "Darts", "PowerLifting", "Marathon");
var answersArray = new Array("fast", "fast", "slow", "fast", "fast", "slow", "slow", "slow");
var letArray = new Array("fast1", "fast2", "slow1", "fast3", "fast4", "slow2", "slow3", "slow4");
var activityTXTArray = new Array("Golf swing", "100m swim", "Boxing punch", "Tennis racquet swing", "Darts throw", "Power lifting", "Marathon");
var arraySmall:Array = smallArray;
var arrayLet:Array = letArray;
var arrayActivity:Array = activityTXTArray;
//var draggable = getChildByName(sportArray[0]);
//setup bonus bar
bonusBar.gotoAndStop(2);
bonusBar.x = timeBar.x;
bonusBar.y = timeBar.y - timeBar.height;
bonusBar.height = timeBar.height/startingLife * lifeBoost;
playerLife = startingLife;
timeBar.height = playerLife * (300/startingLife);
/* listeners */
countdownTimer.addEventListener(TimerEvent.TIMER, timerTick);
countdownTimer.start();
retry_btn.addEventListener(MouseEvent.CLICK, retry);
baseball.addEventListener(MouseEvent.MOUSE_DOWN, clickToDrag1);
stage.addEventListener(MouseEvent.MOUSE_UP, releaseToDrop);
function clickToDrag1(event:MouseEvent):void
{
var draggable = getChildByName(sportArray[sportCounter]);
trace("the counter name is "+ sportArray[sportCounter]);
trace("the baseball name is "+ baseball.name);
trace("the draggable name is "+ draggable.name);
trace("the answer array is "+ answersArray[counter]);
trace("the sport array is "+ sportArray[sportCounter]);
this.setChildIndex(draggable,this.numChildren-1);
draggable.startDrag();
}
function releaseToDrop(event:MouseEvent):void
{
//get current obj name from sportArray
var draggable = getChildByName(sportArray[sportCounter]);
//check if this obj is dropped on the Fast or Slow MovieClip
if(draggable.hitTestObject(getChildByName(answersArray[counter])))
{
//move on to the next F/S answer
counter++;
score++;
var tick = new Tick();
addChild(tick);
tick.x = 370;
tick.y = 200;
activity_txt.text = activityTXTArray.shift();
playerLife += lifeBoost;
timeBar.height = playerLife * (300/startingLife);
bonusBar.gotoAndPlay(2);
var smallName = getChildByName(smallArray.shift());
smallName.visible = true;
var letters = getChildByName(letArray.shift());
letters.visible = true;
//remove the drag listenter on the current object (name assigned via sportArray)
draggable.removeEventListener(MouseEvent.MOUSE_DOWN, clickToDrag1);
//stage.removeEventListener(MouseEvent.MOUSE_UP, releaseToDrop);
//remove the current object
removeChild(getChildByName(sportArray[sportCounter]));
//delete ref
var deleteRef = getChildByName(sportArray[sportCounter]);
deleteRef = null;
//move on to the next one
sportCounter++;
//add a new object
var obj:Class = getDefinitionByName(sportArray[sportCounter]) as Class;
var myMclip = new obj();
//name it
myMclip.name = sportArray[sportCounter];
//var clipName = getChildByName(sportArray[0]);
myMclip.x = myMclip.y = 400;
myMclip.buttonMode = true;
trace("myClip name "+myMclip.name);
addChild(myMclip);
//add listener to new obj (is this removed via draggable?)
myMclip.addEventListener(MouseEvent.MOUSE_DOWN, clickToDrag1);
//stage.addEventListener(MouseEvent.MOUSE_UP, releaseToDrop);
}
else{
draggable.x = startPosX;
draggable.y = startPosY;
draggable.stopDrag();
var cross = new Cross();
addChild(cross);
cross.x = 370;
cross.y = 200
}
}
function timerTick(e:TimerEvent):void {
//removes from 40(life) every half a second
playerLife -= 1;
//bar height = % of whats left of life
timeBar.height = playerLife * (300/startingLife);
bonusBar.y = timeBar.y - timeBar.height;
if(playerLife == 0) {
loseGame();
} else if(playerLife>0 && score > targetScore-1) {
winGame();
}
}
function loseGame():void
{
var removeCurrent = getChildByName(sportArray[sportCounter]);
removeCurrent.visible = false;
hideStuff();
theEnd.visible = true;
theEnd.end_txt.text = "sorry you lost"
retry_btn.visible = true;
//baseball.removeEventListener(MouseEvent.MOUSE_DOWN, clickToDrag1);
//stage.removeEventListener(MouseEvent.MOUSE_UP, releaseToDrop);
//baseball = null;
trace(baseball);
}
function winGame():void
{
var removeCurrent = getChildByName(sportArray[sportCounter]);
removeCurrent.visible = false;
hideStuff();
theEnd.visible = true;
theEnd.end_txt.text = "You've won!"
retry_btn.visible = true;
baseball.removeEventListener(MouseEvent.MOUSE_DOWN, clickToDrag1);
//stage.removeEventListener(MouseEvent.MOUSE_UP, releaseToDrop);
}
function retry(e:MouseEvent):void
{
playerLife = startingLife;
timeBar.height = playerLife * (300/startingLife);
score = 0;
counter = 0;
sportCounter = 0;
countdownTimer.reset();
countdownTimer.start();
var baseball:MovieClip = new Baseball();
baseball.name = "baseball";
trace("the type is "+baseball);
trace("the name is " + baseball.name);
addChild(baseball);
baseball.buttonMode = true;
baseball.x = startPosX;
baseball.y = startPosY;
baseball.addEventListener(MouseEvent.MOUSE_DOWN, clickToDrag1);
//stage.addEventListener(MouseEvent.MOUSE_UP, releaseToDrop);
activity_txt.text = "Swinging a baseball bat";
smallArray = arraySmall;
letArray = arrayLet;
activityTXTArray = arrayActivity;
retry_btn.visible = false;
theEnd.visible = false;
showStuff();
smallArray = new Array("baseballSmall","golfSmall", "swimSmall", "boxingSmall","tennisSmall", "dartsSmall", "powerSmall", "marathonSmall");
var sportArray = new Array("baseball","Golf", "Swimming", "Boxing", "Tennis", "Darts", "PowerLifting", "Marathon");
var answersArray = new Array("fast", "fast", "slow", "fast", "fast", "slow", "slow", "slow");
var letArray = new Array("fast1", "fast2", "slow1", "fast3", "fast4", "slow2", "slow3", "slow4");
var activityTXTArray = new Array("Golf swing", "100m swim", "Boxing punch", "Tennis racquet swing", "Darts throw", "Power lifting", "Marathon");
}
function showStuff():void
{
activity_txt.visible = true;
fast.visible = true;
slow.visible = true;
timeBar.visible = true;
bonusBar.visible = true;
}
function hideStuff():void
{
fast1.visible = false;
fast2.visible = false;
fast3.visible = false;
fast4.visible = false;
slow1.visible = false;
slow2.visible = false;
baseballSmall.visible = false;
golfSmall.visible = false;
swimSmall.visible = false;
boxingSmall.visible = false;
tennisSmall.visible = false;
dartsSmall.visible = false;
activity_txt.visible = false;
fast.visible = false;
slow.visible = false;
timeBar.visible = false;
bonusBar.visible = false;
}
The OP has solved the problem, as is indicated in this comment:
I hadn't removed the object on game end, I had hidden it so when it came around again a duplicate was created. I've removed it instead of hiding and it works fine. Having two clips with on the stage created some strange drag and drop behavior which sort of gave it away. "This is not 'my' answer. MAngoPop answered this in the comments but forgot to add it as an answer."

How to align object timer and score in actionscript3.0 to center

I'm doing a matching game.using adobeFlash, actionScript3.0. I have no idea how to align the Timer n Score to the center when game is on.. n where to insert those coding..
package {
import flash.display.*;
import flash.events.*;
import flash.text.*;
import flash.display.StageAlign;
import flash.utils.getTimer;
import flash.utils.Timer;
import flash.media.Sound;
import flash.media.SoundChannel;
public class MatchingGameObject10 extends MovieClip {
// game constants
private static const boardWidth:uint = 8;
private static const boardHeight:uint = 8;
private static const cardHorizontalSpacing:Number = 52;
private static const cardVerticalSpacing:Number = 52;
private static const boardOffsetX:Number = 300;
private static const boardOffsetY:Number = 200;
private static const pointsForMatch:int = 100;
private static const pointsForMiss:int = -5;
// variables
private var firstCard:Card10;
private var secondCard:Card10;
private var cardsLeft:uint;
private var gameScore:int;
private var gameStartTime:uint;
private var gameTime:uint;
// text fields
private var gameScoreField:TextField;
private var gameTimeField:TextField;
// timer to return cards to face-down
private var flipBackTimer:Timer;
// set up sounds
var theFirstCardSound:FirstCardSound = new FirstCardSound();
var theMissSound:MissSound = new MissSound();
var theMatchSound:MatchSound = new MatchSound();
// initialization function
public function MatchingGameObject10():void {
// make a list of card numbers
var cardlist:Array = new Array();
for(var i:uint=0;i<boardWidth*boardHeight/2;i++) {
cardlist.push(i);
cardlist.push(i);
}
// create all the cards, position them, and assign a randomcard face to each
cardsLeft = 0;
for(var x:uint=0;x<boardWidth;x++) { // horizontal
for(var y:uint=0;y<boardHeight;y++) { // vertical
var c:Card10 = new Card10(); // copy the movie clip
c.stop(); // stop on first frame
c.x = x*cardHorizontalSpacing+boardOffsetX; // set position
c.y = y*cardVerticalSpacing+boardOffsetY;
var r:uint = Math.floor(Math.random()*cardlist.length); // get a random face
c.cardface = cardlist[r]; // assign face to card
cardlist.splice(r,1); // remove face from list
c.addEventListener(MouseEvent.CLICK,clickCard); // have it listen for clicks
c.buttonMode = true;
addChild(c); // show the card
cardsLeft++;
}
}
// set up the score
gameScoreField = new TextField();
//gameScoreField = center;
////gameScoreField = center;
addChild(gameScoreField);
gameScore = 0;
showGameScore();
// set up the clock
gameTimeField = new TextField();
gameTimeField.x = 450;
//gameTimeField = center;
////gameTimeField.stage = StageAlign.TOP;
addChild(gameTimeField);
gameStartTime = getTimer();
gameTime = 0;
addEventListener(Event.ENTER_FRAME,showTime);
}
// player clicked on a card
public function clickCard(event:MouseEvent) {
var thisCard:Card10 = (event.target as Card10); // what card?
if (firstCard == null) { // first card in a pair
firstCard = thisCard; // note it
thisCard.startFlip(thisCard.cardface+2);
playSound(theFirstCardSound);
} else if (firstCard == thisCard) { // clicked first card again
firstCard.startFlip(1);
firstCard = null;
playSound(theMissSound);
} else if (secondCard == null) { // second card in a pair
secondCard = thisCard; // note it
thisCard.startFlip(thisCard.cardface+2);
// compare two cards
if (firstCard.cardface == secondCard.cardface) {
// remove a match
removeChild(firstCard);
removeChild(secondCard);
// reset selection
firstCard = null;
secondCard = null;
// add points
gameScore += pointsForMatch;
showGameScore();
playSound(theMatchSound);
// check for game over
cardsLeft -= 2; // 2 less cards
if (cardsLeft == 0) {
MovieClip(root).gameScore = gameScore;
MovieClip(root).gameTime = clockTime(gameTime);
MovieClip(root).gotoAndStop("gameover");
}
} else {
gameScore += pointsForMiss;
showGameScore();
playSound(theMissSound);
flipBackTimer = new Timer(2000,1);
flipBackTimer.addEventListener(TimerEvent.TIMER_COMPLETE,returnCards);
flipBackTimer.start();
}
} else { // starting to pick another pair
returnCards(null);
playSound(theFirstCardSound);
// select first card in next pair
firstCard = thisCard;
firstCard.startFlip(thisCard.cardface+2);
}
}
// return cards to face-down
public function returnCards(event:TimerEvent) {
firstCard.startFlip(1);
secondCard.startFlip(1);
firstCard = null;
secondCard = null;
flipBackTimer.removeEventListener(TimerEvent.TIMER_COMPLETE,returnCards);
}
public function showGameScore() {
gameScoreField.text = "Score: "+String(gameScore);
}
public function showTime(event:Event) {
gameTime = getTimer()-gameStartTime;
gameTimeField.text = "Time: "+clockTime(gameTime);
}
public function clockTime(ms:int) {
var seconds:int = Math.floor(ms/1000);
var minutes:int = Math.floor(seconds/60);
seconds -= minutes*60;
var timeString:String = minutes+":"+String(seconds+100).substr(1,2);
return timeString;
}
public function playSound(soundObject:Object) {
var channel:SoundChannel = soundObject.play();
}
}
}
When you want to (horizontal) align a DisplayObject in its parent DisplayObject, just do something like this:
child.x = (parent.width - child.width) / 2;
In your code example this would be something like
gameTimeField.x = (this.width - gameTimeField.width) / 2;
This should be inserted, when the child DisplayObject is completely painted, so its width is known.