Now, I do Flash Actionscript 3.0 development, I have a Base64bitEncoder class with the encodeByteArray(...) and decodeToByteArray(...) function.
but when I write the below testing code, but the byteArray.length at before Encoding and after decoding are different, could you help to see see where I wrong?
Test Sample Code as below:
var _loc_2:XML = new XML("<formXML>aaaaa</formXML>");;
var _loc_3:ByteArray;
_loc_3 = new ByteArray();
//_loc_3.endian = Endian.LITTLE_ENDIAN;
_loc_3.writeUTFBytes(_loc_2);
trace("Before Encoding Length:" + _loc_3.length);
var baseString:String = Base64bitEncoder.encodeByteArray(_loc_3);
trace(baseString);
var strArray:ByteArray = Base64bitEncoder.decodeToByteArray(baseString);
trace("After Decoding length:" + strArray.length);
The Output log as below:
Before Encoding Length:5
GFhAGFhA
After Decoding length:**6**
Why the after is 6, where I do wrong?
Base64bitEncoder source code as below:
package
{
import flash.utils.*;
public class Base64bitEncoder extends Object
{
private static const BASE64_CHARS:String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
private static const BASE64_URL_CHARS:String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_!";
public function Base64bitEncoder()
{
return;
}// end function
public static function encode(param1:String, param2:Boolean = false) : String
{
var _loc_3:ByteArray;
_loc_3 = new ByteArray();
_loc_3.writeUTFBytes(param1);
return encodeByteArray(_loc_3, param2);
}// end function
public static function decodeToByteArray(param1:String, param2:Boolean = false) : ByteArray
{
var _loc_3:ByteArray;
var _loc_4:Array;
var _loc_5:Array;
var _loc_6:uint;
var _loc_7:uint;
var _loc_8:uint;
_loc_3 = new ByteArray();
_loc_4 = new Array(4);
_loc_5 = new Array(3);
_loc_6 = 0;
while (_loc_6 < param1.length)
{
// label
_loc_7 = 0;
while (_loc_7++ < 4 && _loc_6 + _loc_7 < param1.length)
{
// label
_loc_4[_loc_7] = param2 == true ? (BASE64_URL_CHARS.indexOf(param1.charAt(_loc_6 + _loc_7))) : (BASE64_CHARS.indexOf(param1.charAt(_loc_6 + _loc_7)));
}// end while
_loc_5[0] = (_loc_4[0] << 2) + ((_loc_4[1] & 48) >> 4);
_loc_5[1] = ((_loc_4[1] & 15) << 4) + ((_loc_4[2] & 60) >> 2);
_loc_5[2] = ((_loc_4[2] & 3) << 6) + _loc_4[3];
_loc_8 = 0;
while (_loc_8++ < _loc_5.length)
{
// label
if (_loc_4[_loc_8 + 1] == 64)
{
break;
}// end if
_loc_3.writeByte(_loc_5[_loc_8]);
}// end while
_loc_6 = _loc_6 + 4;
}// end while
_loc_3.position = 0;
return _loc_3;
}// end function
public static function encodeByteArray(param1:ByteArray, param2:Boolean = false) : String
{
var _loc_3:String;
var _loc_4:Array;
var _loc_5:Array;
var _loc_6:uint;
var _loc_7:uint;
var _loc_8:uint;
_loc_3 = "";
_loc_5 = new Array(4);
param1.position = 0;
while (param1.bytesAvailable > 0)
{
// label
_loc_4 = [];
_loc_6 = 0;
while (_loc_6++ < 3 && param1.bytesAvailable > 0)
{
// label
_loc_4[_loc_6] = param1.readUnsignedByte();
}// end while
_loc_5[0] = (_loc_4[0] & 252) >> 2;
_loc_5[1] = (_loc_4[0] & 3) << 4 | _loc_4[1] >> 4;
_loc_5[2] = (_loc_4[1] & 15) << 2 | _loc_4[2] >> 6;
_loc_5[3] = _loc_4[2] & 63;
_loc_7 = _loc_4.length;
while (_loc_7++ < 3)
{
// label
_loc_5[_loc_7 + 1] = 64;
}// end while
_loc_8 = 0;
while (_loc_8++ < _loc_5.length)
{
// label
_loc_3 = _loc_3 + (param2 == true ? (BASE64_URL_CHARS.charAt(_loc_5[_loc_8])) : (BASE64_CHARS.charAt(_loc_5[_loc_8])));
}// end while
}// end while
return _loc_3;
}// end function
public static function decode(param1:String, param2:Boolean = false) : String
{
var _loc_3:ByteArray;
_loc_3 = decodeToByteArray(param1, param2);
return _loc_3.readUTFBytes(_loc_3.length);
}// end function
}
}
#akmozo, today, I spend some time to study the base64 theory, I fix the issue, thanks.
update code:
public static function encodeByteArray(param1:ByteArray, param2:Boolean = false) : String
{
var _loc_3:String;
var _loc_4:Array;
var _loc_5:Array;
var _loc_6:uint;
var _loc_7:uint;
var _loc_8:uint;
_loc_3 = "";
_loc_5 = new Array(4);
param1.position = 0;
while (param1.bytesAvailable > 0)
{
// label
_loc_4 = [];
_loc_6 = 0;
while (_loc_6 < 3 && param1.bytesAvailable > 0)
{
// label
_loc_4[_loc_6] = param1.readUnsignedByte();
_loc_6 = _loc_6 + 1;
}// end while
_loc_5[0] = (_loc_4[0] & 252) >> 2;
_loc_5[1] = (_loc_4[0] & 3) << 4 | _loc_4[1] >> 4;
_loc_5[2] = (_loc_4[1] & 15) << 2 | _loc_4[2] >> 6;
_loc_5[3] = _loc_4[2] & 63;
/*
trace("--------------------------------");
trace("0:" + _loc_4[0]);
trace("1:" + _loc_4[1]);
trace("3:" + _loc_4[2]);
trace("len:"+_loc_4.length);
trace("++++++++++++++++++++++++++++++++");
*/
_loc_7 = _loc_4.length;
while (_loc_7 < 3)
{
// label
_loc_5[_loc_7 + 1] = 64;
_loc_7 = _loc_7 + 1;
}// end while
_loc_8 = 0;
while (_loc_8 < _loc_5.length)
{
// label
_loc_3 = _loc_3 + (param2 == true ? (BASE64_URL_CHARS.charAt(_loc_5[_loc_8])) : (BASE64_CHARS.charAt(_loc_5[_loc_8])));
_loc_8 = _loc_8 + 1;
}// end while
}// end while
return _loc_3;
}// end function
public static function decodeToByteArray(param1:String, param2:Boolean = false) : ByteArray
{
var _loc_3:ByteArray;
var _loc_4:Array;
var _loc_5:Array;
var _loc_6:uint;
var _loc_7:uint;
var _loc_8:uint;
_loc_3 = new ByteArray();
_loc_4 = new Array(4);
_loc_5 = new Array(3);
_loc_6 = 0;
while (_loc_6 < param1.length)
{
// label
_loc_7 = 0;
while (_loc_7 < 4 && _loc_6 + _loc_7 < param1.length)
{
// label
_loc_4[_loc_7] = param2 == true ? (BASE64_URL_CHARS.indexOf(param1.charAt(_loc_6 + _loc_7))) : (BASE64_CHARS.indexOf(param1.charAt(_loc_6 + _loc_7)));
_loc_7 = _loc_7 + 1;
}// end while
_loc_5[0] = (_loc_4[0] << 2) + ((_loc_4[1] & 48) >> 4);
_loc_5[1] = ((_loc_4[1] & 15) << 4) + ((_loc_4[2] & 60) >> 2);
_loc_5[2] = ((_loc_4[2] & 3) << 6) + _loc_4[3];
_loc_8 = 0;
while (_loc_8 < _loc_5.length)
{
// label
if (_loc_4[_loc_8 + 1] == 64)
{
break;
}// end if
_loc_3.writeByte(_loc_5[_loc_8]);
_loc_8 = _loc_8 + 1;
}// end while
_loc_6 = _loc_6 + 4;
}// end while
_loc_3.position = 0;
return _loc_3;
}// end function
Related
I'm having trouble with the following function:
private function whichLevelToLoad():void{
if(levelToLoad == "nothing"){
currentLevel = null;
}
var thisObj:Object = new Object();
if(levelBtnArray!=null){
for(var j:int=levelBtnArray.length-1;j>=0;j--) {
if(levelToLoad == String("level " + (j+1))){
thisObj["level"+(j+1)] = new ["Level"+(j+1)]();--------------------------->The Problem
thisObj["level" + (j+1)].x = 0;
thisObj["level" + (j+1)].y = 0;
addChildAt(thisObj["level" + (j+1)], 0);
currentLevel = thisObj["level" + (j+1)];
}
}
}
}
I'm trying to instatiate 75 objects by using a loop. The line would look like this,"thisObj.level1 = new Level1(); with the numbers going from 1-75. Is this possible? How
do I do it?
Try
if(levelBtnArray!=null){
var levelClass:Class;
for(var j:int=levelBtnArray.length-1;j>=0;j--) {
if(levelToLoad == String("level " + (j+1))){
levelClass = getDefinitionByName( "Level"+(j+1) ) as Class;
thisObj["level"+(j+1)] = new levelClass();
thisObj["level" + (j+1)].x = 0;
thisObj["level" + (j+1)].y = 0;
addChildAt(thisObj["level" + (j+1)], 0);
currentLevel = thisObj["level" + (j+1)];
}
}
}
I dont know if the question represent exactly what i want to ask, but i could`t figure how else to name what i want to do.
So im trying to create a 3x3 grid and in every cell i have a image.
I can click on any of the 2nd cell of a column only if the 1st of the same column has been clicked before that.What i mean is
i can click cell No:5 only if 4 has been clicked before that
i can click cell No:9 only if 8 and 7 has been clicked before that
i can click cell No:1,4,7 anytime.
and also when they are clicked their alpha gets to 0.1 (so i know that the cell has been clicked.)
currently i the logic for creating the grid and changing the alpha of any object i click but i don`t have the logic for the hierarchy.
public function Main()
{
var index:int = 0
var col:int = 3
var row:int = 3
for (var i:int = 0; i < row; i++)
{
for (var j:int = 0; j < col; j++)
{
var cls:Class = Class(getDefinitionByName("Bitmap" + (index + 1) ));
myImage = new Bitmap( new cls() );
var myImage_mc:MovieClip = new MovieClip();
myImage_mc.addChild(myImage)
myImage_mc.x = 100 + i * (myImage_mc.width + 10)
myImage_mc.y = 100 + j * (myImage_mc.height + 10)
this.addChild(myImage_mc);
myImage_mc.mouseEnabled = true
myImage_mc.addEventListener(MouseEvent.CLICK, onClick);
index++
}
}
}
private function onClick(ev:MouseEvent):void
{
trace(ev.target.name)
ev.currentTarget.alpha = 0.1;
}
Another way is to use a link variable to link element.
public class BitmapButton extends Sprite {
public var next:BitmapButton = null;
}
public class Main extends Sprite {
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
var index:int = 0
var col:int = 3
var row:int = 3
for (var j:int = 0; j < col; j++)
{
var lastRowElement:BitmapButton = null;
for (var i:int = 0; i < row; i++)
{
var bmpd:BitmapData = new BitmapData( 100, 100, false, Math.floor( 0xff + Math.random() * 0xffffff ) );
var myImage:Bitmap = new Bitmap( bmpd );
var myImage_mc:BitmapButton = new BitmapButton();
myImage_mc.addChild(myImage)
myImage_mc.x = 100 + j * (myImage_mc.width + 10);
myImage_mc.y = 100 + i * (myImage_mc.height + 10);
myImage_mc.name = i + "_" + j;
this.addChild(myImage_mc);
myImage_mc.mouseChildren = false;
myImage_mc.mouseEnabled = false;
myImage_mc.addEventListener(MouseEvent.CLICK, onClick);
if ( lastRowElement == null ) {
myImage_mc.mouseEnabled = true;
} else {
lastRowElement.next = myImage_mc;
}
lastRowElement = myImage_mc;
index++
}
}
}
private function onClick(ev:MouseEvent):void
{
trace(ev.target.name)
if ( ev.target is BitmapButton ) {
var btn:BitmapButton = ev.target as BitmapButton;
ev.currentTarget.alpha = 0.1;
if( btn.next != null ) {
btn.next.mouseEnabled = true;
}
}
}
}
Something like this
package
{
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.utils.Dictionary;
public class Grid extends Sprite
{
private var _box : Dictionary = new Dictionary;
private var _conditions : Dictionary = new Dictionary;
private var _clicked : Dictionary = new Dictionary;
public function Grid()
{
var index:int = 0
var col:int = 3
var row:int = 3
for (var i:int = 0; i < row; i++)
{
for (var j:int = 0; j < col; j++)
{
//var cls:Class = Class(getDefinitionByName("Bitmap" + (index + 1) ));
//myImage = new Bitmap( new cls() );
var myImage_mc:MovieClip = new MovieClip();
//myImage_mc.addChild(myImage)
_box[ index ] = myImage_mc;
// Conditions for the 2nd and 3nd line etc.
if( i != 0 )
_conditions[ myImage_mc ] = _box[ index - col ];
// ------ DEBUG
myImage_mc.graphics.beginFill( 0xFFFFFF * Math.random() );
myImage_mc.graphics.drawRect(0,0,100,100);
myImage_mc.graphics.endFill();
// ------ END DEBUG
// Note i / j are invert from your example
myImage_mc.x = 100 + j * (myImage_mc.width + 10);
myImage_mc.y = 100 + i * (myImage_mc.height + 10);
this.addChild(myImage_mc);
myImage_mc.mouseEnabled = true
myImage_mc.addEventListener(MouseEvent.CLICK, onClick);
index++
}
}
}
protected function onClick(ev:MouseEvent):void
{
var neededClickElement : MovieClip = _conditions[ ev.currentTarget ];
// Check if we need an active movieclip before
if( neededClickElement != null && ! _clicked[ neededClickElement ] )
return;
_clicked[ ev.currentTarget ] = true;
ev.currentTarget.alpha = 0.1;
}
}
}
So, the same college project as last time, and this time im having a trouble with undefined methods. The entirity of my code is below
package
{
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class Lesson_05 extends MovieClip
{
private static const boardWidth:uint = 4;
private static const boardHeight:uint = 2;
private static const cardHorizontalSpacing:Number = 52;
private static const cardVerticalSpacing:Number = 52;
private static const boardOffsetX:Number = 171;
private static const boardOffsetY:Number = 148;
private var firstCard:Card;
private var secondCard:Card;
private var cardsLeft:uint;
var startPage:StartPage_1;
var matchPage:MatchPage_1;
var guessPage:GuessPage_1;
var startMessage:String;
var mysteryNumber:uint;
var currentGuess:uint;
var guessesRemaining:uint;
var guessesMade:uint;
var gameStatus:String;
var gameWon:Boolean;
public function Lesson_05():void
{
startPage = new StartPage_1();
matchPage = new MatchPage_1();
guessPage = new GuessPage_1();
addChild(startPage);
startPage.matchButton.addEventListener(MouseEvent.CLICK,onMatchButtonClick);
startPage.guessButton_1.addEventListener(MouseEvent.CLICK,onGuessButtonClick_1);
guessPage.matchButton.addEventListener(MouseEvent.CLICK,onMatchButtonClick_Guess);
//Output Errors #2025 when added this line;
guessPage.startButton.addEventListener(MouseEvent.CLICK,onStartButtonClick);
matchPage.startButton.addEventListener(MouseEvent.CLICK,onStartButtonClick_Match);
matchPage.guessButton_1.addEventListener(MouseEvent.CLICK,onGuessButtonClick_Match_1);
}
function onMatchButtonClick(event:MouseEvent):void
{
addChild(matchPage);
removeChild(startPage);
match();
}
function onGuessButtonClick_1(event:MouseEvent):void
{
addChild(guessPage);
removeChild(startPage);
guess();
}
function onMatchButtonClick_Guess(event:MouseEvent):void
{
addChild(matchPage);
removeChild(guessPage);
match();
}
function onStartButtonClick(event:MouseEvent):void
{
addChild(startPage);
removeChild(guessPage);
}
function onStartButtonClick_Match(event:MouseEvent):void
{
addChild(startPage);
removeChild(matchPage);
}
function onGuessButtonClick_Match_1(event:MouseEvent):void
{
addChild(guessPage);
removeChild(matchPage);
guess();
}
function guess():void
{
startMessage = "I am thinking of a number between 1 and 20";
mysteryNumber = Math.ceil(Math.random()*20);
guessesRemaining = 10;
guessesMade = 0;
gameStatus = "";
gameWon = false;
guessPage.output_txt.text = startMessage;
guessPage.input_txt.text = "";
guessPage.input_txt.backgroundColor = 0xFFCCCCCC;
guessPage.input_txt.restrict = "0-9";
guessPage.stage.focus = guessPage.input_txt;
guessPage.guessButton_2.enabled = true;
guessPage.guessButton_2.alpha = 1;
guessPage.againButton_1.visible = false;
guessPage.guessButton_2.addEventListener(MouseEvent.CLICK,onGuessButtonClick_2);
}
function onGuessButtonClick_2(event:MouseEvent):void
{
guessesRemaining--;
guessesMade++;
gameStatus = "Guesses Remaining: " + guessesRemaining + ", GuessesMade:" + guessesMade;
currentGuess = uint(guessPage.input_txt.text);
if (currentGuess > mysteryNumber)
{
guessPage.output_txt.text = "That's too high!" + "\n" + gameStatus;
checkGameOver();
}
else if (currentGuess < mysteryNumber)
{
guessPage.output_txt.text = "That's too low!" + "\n" + gameStatus;
checkGameOver();
}
else
{
//guessPage.output_txt.text = "Well Done! You got it!";
gameWon = true;
endGame();
}
function checkGameOver():void
{
if (guessesRemaining < 1)
{
endGame();
}
}
function endGame():void
{
if (gameWon)
{
guessPage.output_txt.text = "Yes, it's " + mysteryNumber + "!" + "\n" + "It only took you " + guessesMade + " guesses!";
}
else
{
guessPage.output_txt.text = "Sorry, you've run out of guesses!" + "\n" + "The correct number was " + mysteryNumber + ".";
}
guessPage.guessButton_2.removeEventListener(MouseEvent.CLICK,onGuessButtonClick_2);
guessPage.guessButton_2.enabled = false;
guessPage.guessButton_2.alpha = 0.5;
guessPage.againButton_1.visible = true;
guessPage.againButton_1.addEventListener(MouseEvent.CLICK,onAgainButtonClick_1);
}
function onAgainButtonClick_1(event:MouseEvent):void
{
guess();
guessPage.againButton_1.removeEventListener(MouseEvent.CLICK,onAgainButtonClick_1);
}
function match():void
{
var cardlist:Array = new Array();
for (var i:uint=0; i<boardWidth*boardHeight/2; i++)
{
cardlist.push(i);
cardlist.push(i);
}
cardsLeft = 0;
for (var x:uint=0; x<boardWidth; x++)
{
for (var y:uint=0; y<boardHeight; y++)
{
var c:Card = new Card();
c.stop();
c.x = x * cardHorizontalSpacing + boardOffsetX;
c.y = y * cardVerticalSpacing + boardOffsetY;
var r:uint = Math.floor(Math.random() * cardlist.length);
c.cardface = cardlist[r];
cardlist.splice(r,1);
c.addEventListener(MouseEvent.CLICK,clickCard);
addChild(c);
cardsLeft++;
}
}
}
function clickCard(event:MouseEvent)
{
var thisCard:Card = (event.target as Card);
if (firstCard ==null)
{
firstCard = thisCard;
firstCard.gotoAndStop(thisCard.cardface+2);
}
else if (firstCard ==thisCard)
{
firstCard.gotoAndstop(1);
firstCard = null;
}
else if (secondCard == null)
{
secondCard = thisCard;
secondCard.gotoAndStop(thisCard.cardface+2);
if (firstCard.cardface == secondCard.cardface)
{
removeChild(firstCard);
removeChild(secondCard);
firstCard = null;
secondCard = null;
cardsLeft -= 2;
if (cardsLeft ==0)
{
}
}
}
else
{
firstCard.gotoAndStop(1);
secondCard.gotoAndStop(1);
secondCard = null;
firstCard = thisCard;
firstCard.gotoAndStop(thisCard.cardface+2);
}
}
}
}
}
and the problems I'm getting are:
1180: Call to a possibly undefined method match.
I have very little knowledge with AS3, and have spent more than 2 hours trying to resolve this problem. Thanks in advance guys, PS any links to tutorials etc are massively appreciated
You misplaced close braces'}' of the function onGuessButtonClick_2. Use the below code and check it.
package
{
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class Lesson_05 extends MovieClip
{
private static const boardWidth:uint = 4;
private static const boardHeight:uint = 2;
private static const cardHorizontalSpacing:Number = 52;
private static const cardVerticalSpacing:Number = 52;
private static const boardOffsetX:Number = 171;
private static const boardOffsetY:Number = 148;
private var firstCard:Card;
private var secondCard:Card;
private var cardsLeft:uint;
var startPage:StartPage_1;
var matchPage:MatchPage_1;
var guessPage:GuessPage_1;
var startMessage:String;
var mysteryNumber:uint;
var currentGuess:uint;
var guessesRemaining:uint;
var guessesMade:uint;
var gameStatus:String;
var gameWon:Boolean;
public function Lesson_05():void
{
startPage = new StartPage_1();
matchPage = new MatchPage_1();
guessPage = new GuessPage_1();
addChild(startPage);
startPage.matchButton.addEventListener(MouseEvent.CLICK,onMatchButtonClick);
startPage.guessButton_1.addEventListener(MouseEvent.CLICK,onGuessButtonClick_1);
guessPage.matchButton.addEventListener(MouseEvent.CLICK,onMatchButtonClick_Guess);
//Output Errors #2025 when added this line;
guessPage.startButton.addEventListener(MouseEvent.CLICK,onStartButtonClick);
matchPage.startButton.addEventListener(MouseEvent.CLICK,onStartButtonClick_Match);
matchPage.guessButton_1.addEventListener(MouseEvent.CLICK,onGuessButtonClick_Match_1);
}
function onMatchButtonClick(event:MouseEvent):void
{
addChild(matchPage);
removeChild(startPage);
match();
}
function onGuessButtonClick_1(event:MouseEvent):void
{
addChild(guessPage);
removeChild(startPage);
guess();
}
function onMatchButtonClick_Guess(event:MouseEvent):void
{
addChild(matchPage);
removeChild(guessPage);
match();
}
function onStartButtonClick(event:MouseEvent):void
{
addChild(startPage);
removeChild(guessPage);
}
function onStartButtonClick_Match(event:MouseEvent):void
{
addChild(startPage);
removeChild(matchPage);
}
function onGuessButtonClick_Match_1(event:MouseEvent):void
{
addChild(guessPage);
removeChild(matchPage);
guess();
}
function guess():void
{
startMessage = "I am thinking of a number between 1 and 20";
mysteryNumber = Math.ceil(Math.random()*20);
guessesRemaining = 10;
guessesMade = 0;
gameStatus = "";
gameWon = false;
guessPage.output_txt.text = startMessage;
guessPage.input_txt.text = "";
guessPage.input_txt.backgroundColor = 0xFFCCCCCC;
guessPage.input_txt.restrict = "0-9";
guessPage.stage.focus = guessPage.input_txt;
guessPage.guessButton_2.enabled = true;
guessPage.guessButton_2.alpha = 1;
guessPage.againButton_1.visible = false;
guessPage.guessButton_2.addEventListener(MouseEvent.CLICK,onGuessButtonClick_2);
}
function onGuessButtonClick_2(event:MouseEvent):void
{
guessesRemaining--;
guessesMade++;
gameStatus = "Guesses Remaining: " + guessesRemaining + ", GuessesMade:" + guessesMade;
currentGuess = uint(guessPage.input_txt.text);
if (currentGuess > mysteryNumber)
{
guessPage.output_txt.text = "That's too high!" + "\n" + gameStatus;
checkGameOver();
}
else if (currentGuess < mysteryNumber)
{
guessPage.output_txt.text = "That's too low!" + "\n" + gameStatus;
checkGameOver();
}
else
{
//guessPage.output_txt.text = "Well Done! You got it!";
gameWon = true;
endGame();
}
}
function checkGameOver():void
{
if (guessesRemaining < 1)
{
endGame();
}
}
function endGame():void
{
if (gameWon)
{
guessPage.output_txt.text = "Yes, it's " + mysteryNumber + "!" + "\n" + "It only took you " + guessesMade + " guesses!";
}
else
{
guessPage.output_txt.text = "Sorry, you've run out of guesses!" + "\n" + "The correct number was " + mysteryNumber + ".";
}
guessPage.guessButton_2.removeEventListener(MouseEvent.CLICK,onGuessButtonClick_2);
guessPage.guessButton_2.enabled = false;
guessPage.guessButton_2.alpha = 0.5;
guessPage.againButton_1.visible = true;
guessPage.againButton_1.addEventListener(MouseEvent.CLICK,onAgainButtonClick_1);
}
function onAgainButtonClick_1(event:MouseEvent):void
{
guess();
guessPage.againButton_1.removeEventListener(MouseEvent.CLICK,onAgainButtonClick_1);
}
function match():void
{
var cardlist:Array = new Array();
for (var i:uint=0; i<boardWidth*boardHeight/2; i++)
{
cardlist.push(i);
cardlist.push(i);
}
cardsLeft = 0;
for (var x:uint=0; x<boardWidth; x++)
{
for (var y:uint=0; y<boardHeight; y++)
{
var c:Card = new Card();
c.stop();
c.x = x * cardHorizontalSpacing + boardOffsetX;
c.y = y * cardVerticalSpacing + boardOffsetY;
var r:uint = Math.floor(Math.random() * cardlist.length);
c.cardface = cardlist[r];
cardlist.splice(r,1);
c.addEventListener(MouseEvent.CLICK,clickCard);
addChild(c);
cardsLeft++;
}
}
}
function clickCard(event:MouseEvent)
{
var thisCard:Card = (event.target as Card);
if (firstCard ==null)
{
firstCard = thisCard;
firstCard.gotoAndStop(thisCard.cardface+2);
}
else if (firstCard ==thisCard)
{
firstCard.gotoAndstop(1);
firstCard = null;
}
else if (secondCard == null)
{
secondCard = thisCard;
secondCard.gotoAndStop(thisCard.cardface+2);
if (firstCard.cardface == secondCard.cardface)
{
removeChild(firstCard);
removeChild(secondCard);
firstCard = null;
secondCard = null;
cardsLeft -= 2;
if (cardsLeft ==0)
{
}
}
}
else
{
firstCard.gotoAndStop(1);
secondCard.gotoAndStop(1);
secondCard = null;
firstCard = thisCard;
firstCard.gotoAndStop(thisCard.cardface+2);
}
}
}
}
Hope it helps.
Your match() function is inside the onGuessButtonClick_2(MouseEvent) function.
So when you try it from anywhere else that is not the inside of the onGuessButtonClick_2 function, flash gets confused because it cannot find the function match() because it is only callable inside the onGuessButtonClick_2() function.
1180 errors are caused by Flash being dead.
1180 Error: Flash is Dead, please convert your code to HTML5 or do it natively.
okthxbai
Adobe
Evtim wrote a greate customer FlowLayout, and Maxim also have a nice customer Horizontal Multiline Layout but they don't support drag-drop. When define customer
Layout which extends LayoutBase with drag-drop supporting, user should implements or override which functions?
I had worked out, following is my customer layout based on Horizontal Multiline Layouy of Maxim:
public class HorizontalMultilineLayout extends LayoutBase {
private var _rowCount:int = -1;
private var _columnCount:int = -1;
//container width
private var lastWidth:Number = -1;
private var _horizontalGap:Number = 6;
public function get horizontalGap():Number
{
return _horizontalGap;
}
public function set horizontalGap(value:Number):void
{
if (value == _horizontalGap)
return;
_horizontalGap = value;
invalidateTargetSizeAndDisplayList();
}
private var _verticalGap:Number = 6;
public function get verticalGap():Number
{
return _verticalGap;
}
public function set verticalGap(value:Number):void
{
if (value == _verticalGap)
return;
_verticalGap = value;
invalidateTargetSizeAndDisplayList();
}
private var _verticalAlign:String = VerticalAlign.TOP;
[Inspectable(category="General", enumeration="top,bottom,middle", defaultValue="top")]
public function get verticalAlign():String
{
return _verticalAlign;
}
public function set verticalAlign(value:String):void
{
if (_verticalAlign == value)
return;
_verticalAlign = value;
invalidateTargetSizeAndDisplayList();
}
private var _paddingLeft:Number = 0;
public function get paddingLeft():Number
{
return _paddingLeft;
}
public function set paddingLeft(value:Number):void
{
if (_paddingLeft == value)
return;
_paddingLeft = value;
invalidateTargetSizeAndDisplayList();
}
private var _paddingRight:Number = 0;
public function get paddingRight():Number
{
return _paddingRight;
}
public function set paddingRight(value:Number):void
{
if (_paddingRight == value)
return;
_paddingRight = value;
invalidateTargetSizeAndDisplayList();
}
private var _paddingTop:Number = 0;
public function get paddingTop():Number
{
return _paddingTop;
}
public function set paddingTop(value:Number):void
{
if (_paddingTop == value)
return;
_paddingTop = value;
invalidateTargetSizeAndDisplayList();
}
private var _paddingBottom:Number = 0;
public function get paddingBottom():Number
{
return _paddingBottom;
}
public function set paddingBottom(value:Number):void
{
if (_paddingBottom == value)
return;
_paddingBottom = value;
invalidateTargetSizeAndDisplayList();
}
override public function measure():void
{
if (lastWidth == -1) {
return;
}
var measuredWidth:Number = 0;
var measuredMinWidth:Number = 0;
var measuredHeight:Number = 0;
var measuredMinHeight:Number = 0;
var layoutTarget:GroupBase = target;
var n:int = layoutTarget.numElements;
var element:ILayoutElement;
var i:int;
var width:Number = layoutTarget.explicitWidth;
if (isNaN(width) && lastWidth != -1)
width = lastWidth;
if (isNaN(width)) // width is not defined by parent or user
{
// do not specify measuredWidth and measuredHeight to real
// values because in fact we can layout at any width or height
for (i = 0; i < n; i++)
{
element = useVirtualLayout ? layoutTarget.getVirtualElementAt(i) : layoutTarget.getElementAt(i);
if (!element || !element.includeInLayout)
continue;
measuredWidth = Math.ceil(element.getPreferredBoundsWidth());
measuredHeight = Math.ceil(element.getPreferredBoundsHeight());
break;
}
measuredMinWidth = measuredWidth;
measuredMinHeight = measuredHeight;
}
else
{
// calculate lines based on width
var currentLineWidth:Number = 0;
var currentLineHeight:Number = 0;
var lineNum:int = 1;
for (i = 0; i < n; i++)
{
element = useVirtualLayout ? layoutTarget.getVirtualElementAt(i) : layoutTarget.getElementAt(i);
if (!element || !element.includeInLayout)
continue;
var widthWithoutPaddings:Number = width - _paddingLeft - _paddingRight;
var elementWidth:Number = Math.ceil(element.getPreferredBoundsWidth());
if (currentLineWidth == 0 ||
currentLineWidth + _horizontalGap + elementWidth <= widthWithoutPaddings)
{
currentLineWidth += elementWidth + (currentLineWidth == 0 ? 0 : _horizontalGap);
currentLineHeight = Math.max(currentLineHeight, Math.ceil(element.getPreferredBoundsHeight()));
}
else
{
measuredHeight += currentLineHeight;
lineNum++;
currentLineWidth = elementWidth;
currentLineHeight = Math.ceil(element.getPreferredBoundsHeight());
}
}
measuredHeight += currentLineHeight;
if (lineNum > 1)
measuredHeight += _verticalGap * (lineNum - 1);
// do not set measuredWidth to real value because really we can
// layout at any width
for (i = 0; i < n; i++)
{
element = useVirtualLayout ? layoutTarget.getVirtualElementAt(i) : layoutTarget.getElementAt(i);
if (!element || !element.includeInLayout)
continue;
measuredWidth =
measuredMinWidth = Math.ceil(element.getPreferredBoundsWidth());
break;
}
measuredMinHeight = measuredHeight;
}
layoutTarget.measuredWidth = measuredWidth + _paddingLeft + _paddingRight;
layoutTarget.measuredMinWidth = measuredMinWidth + _paddingLeft + _paddingRight;
layoutTarget.measuredHeight = measuredHeight + _paddingTop + _paddingBottom;
layoutTarget.measuredMinHeight = measuredMinHeight + _paddingTop + _paddingBottom;
}
override public function updateDisplayList(width:Number, height:Number):void
{
resetRowAndColumn();
var layoutTarget:GroupBase = target;
var n:int = layoutTarget.numElements;
var element:ILayoutElement;
var i:int;
// calculate lines based on width
var x:Number = _paddingLeft;
var y:Number = _paddingTop;
var maxLineHeight:Number = 0;
var elementCounter:int = 0;
var positions:Vector.<Point> = new Vector.<Point>();
for (i = 0; i < n; i++)
{
element = useVirtualLayout ? layoutTarget.getVirtualElementAt(i) : layoutTarget.getElementAt(i);
if (!element || !element.includeInLayout)
continue;
var elementWidth:Number = Math.ceil(element.getPreferredBoundsWidth());
if (x == _paddingLeft || x + _horizontalGap + elementWidth <= width - _paddingRight) {
if (elementCounter > 0)
x += _horizontalGap;
positions[i] = new Point(x, y);
element.setLayoutBoundsSize(NaN, NaN);
maxLineHeight = Math.max(maxLineHeight, Math.ceil(element.getPreferredBoundsHeight()));
//calculate column count
if(!startNewLine) {
_columnCount++;
}
}else {
startNewLine = true;
_rowCount++;
x = _paddingLeft;
y += _verticalGap + maxLineHeight;
maxLineHeight = Math.ceil(element.getPreferredBoundsHeight());
positions[i] = new Point(x, y);
element.setLayoutBoundsSize(NaN, NaN);
}
x += elementWidth;
elementCounter++;
}
// verticalAlign and setLayoutBoundsPosition() for elements
var yAdd:Number = 0;
var yDifference:Number = height - (y + maxLineHeight + _paddingBottom);
if (_verticalAlign == VerticalAlign.MIDDLE)
yAdd = Math.round(yDifference / 2);
else if (_verticalAlign == VerticalAlign.BOTTOM)
yAdd = Math.round(yDifference);
for (i = 0; i < n; i++)
{
element = useVirtualLayout ? layoutTarget.getVirtualElementAt(i) : layoutTarget.getElementAt(i);
if (!element || !element.includeInLayout)
continue;
var point:Point = positions[i];
point.y += yAdd;
element.setLayoutBoundsPosition(point.x, point.y);
}
// if width changed then height will change too - remeasure
if (lastWidth == -1 || lastWidth != width)
{
lastWidth = width;
invalidateTargetSizeAndDisplayList();
}
//trace("rowCount: ---", _rowCount, " ---columnCount: ----", _columnCount);
}
private var startNewLine:Boolean = false;
private function resetRowAndColumn():void {
var num:int = target.numElements;
if(num >0) {
_rowCount = 1;
_columnCount = 0;
} else {
_rowCount = _columnCount = 0;
}
startNewLine = false;
}
private function invalidateTargetSizeAndDisplayList():void
{
var g:GroupBase = target;
if (!g)
return;
g.invalidateSize();
g.invalidateDisplayList();
}
private function calculateDropCellIndex(x:Number, y:Number):Array {
var xStart:Number = x - paddingLeft;
var yStart:Number = y - paddingTop;
var column:int = Math.floor(xStart / (columnWidth + _horizontalGap));
var row:int = Math.floor(yStart / (rowHeight + _verticalGap));
// Check whether x is closer to left column or right column:
var midColumnLine:Number;
var midRowLine:Number;
midColumnLine = (column + 1) * (columnWidth + _horizontalGap) - _horizontalGap - columnWidth / 2;
// Mid-line is at the middle of the gap between the rows
midRowLine = (row + 1) * (rowHeight + _verticalGap) - _verticalGap / 2;
if (xStart > midColumnLine)
column++;
if (yStart > midRowLine)
row++;
// Limit row and column, if any one is too far from the drop location
// And there is white space
if (column > _columnCount || row > _rowCount)
{
row = _rowCount;
column = _columnCount;
}
if (column < 0)
column = 0;
if (row < 0)
row = 0;
if (row >= _rowCount)
row = _rowCount - 1;
return [row, column];
}
override protected function calculateDropIndex(x:Number, y:Number):int {
var result:Array = calculateDropCellIndex(x, y);
var row:int = result[0];
var column:int = result[1];
var index:int = row * _columnCount + column;
var count:int = target.numElements;
if (index > count) {
index = count;
}
return index;
}
private function get columnWidth():Number {
return typicalLayoutElement.getLayoutBoundsWidth();
}
private function get rowHeight():Number {
return typicalLayoutElement.getLayoutBoundsHeight();
}
//NOTE:we do NOT use contentWidth
override protected function calculateDropIndicatorBounds(dropLocation:DropLocation):Rectangle {
var result:Array = calculateDropCellIndex(dropLocation.dropPoint.x, dropLocation.dropPoint.y);
var row:int = result[0];
var column:int = result[1];
var count:int = target.numElements;
// The last row may be only partially full,
// don't drop beyond the last column.
if (row * _columnCount + column > count)
column = count - (_rowCount - 1) * _columnCount;
var x:Number;
var y:Number;
var dropIndicatorElement:IVisualElement;
var emptySpace:Number; // empty space between the elements
// Start with the dropIndicator dimensions, in case it's not
// an IVisualElement
var width:Number = dropIndicator.width;
var height:Number = dropIndicator.height;
emptySpace = (0 < _horizontalGap ) ? _horizontalGap : 0;
var emptySpaceLeft:Number = column * (columnWidth + _horizontalGap) - emptySpace;
// Special case - if we have negative gap and we're the last column,
// adjust the emptySpaceLeft
if (_horizontalGap < 0 && (column == _columnCount || count == dropLocation.dropIndex))
emptySpaceLeft -= _horizontalGap;
width = emptySpace;
height = rowHeight;
// Special case - if we have negative gap and we're not the last
// row, adjust the height
if (_verticalGap < 0 && row < _rowCount - 1)
height += _verticalGap + 1;
if (dropIndicator is IVisualElement)
{
dropIndicatorElement = IVisualElement(dropIndicator);
width = Math.max(Math.min(width,
dropIndicatorElement.getMaxBoundsWidth(false)),
dropIndicatorElement.getMinBoundsWidth(false));
}
x = emptySpaceLeft + Math.round((emptySpace - width) / 2) + paddingLeft;
// Allow 1 pixel overlap with container border
//x = Math.max(-1, Math.min(target.contentWidth - width + 1, x));
//NOTE:we do NOT use contentWidth
x = Math.max(-1, Math.min(target.width - width + 1, x));
y = row * (rowHeight + _verticalGap) + paddingTop;
return new Rectangle(x, y, width, height);
}
}
For some reason, the last line of my wordwrap does not display
public class ShopButton extends Entity
{
private var callback:Function = null;
public var buttonText:String = "";
public var textImage:Text;
public var picture:BitmapData = new BitmapData(100, 100, true, 1);
public var boxImage:Stamp;
public var displayImage:Graphiclist;
public var displayImage2:Graphiclist;
public var tooltipPicture:BitmapData = new BitmapData(150, 130, true, 1);
public var tooltipBox:Stamp;
public var tooltipText:Array = [];
public var tooltipString:Array = [];
public var buttonWidth:int = 100;
public var buttonHeight:int = 100;
private var clicked:Boolean = false;
private var shiftClicked:Boolean = false;
private var hovered:Boolean = false;
private var fontSize:int = 16;
public var label:String = "";
public function ShopButton(x:int = 0, y:int = 0, label:String = "", callback:Function = null)
{
this.callback = callback;
this.label = label;
buttonText = label;
textImage = new Text(buttonText, 5, 5, { width:buttonWidth-10, wordWrap:true, align:"center", size:fontSize, font:"Abscissa" } );
Draw.setTarget(picture);
Draw.rectPlus(0, 0, buttonWidth, buttonHeight, 0x000000, 1, true, 2, 5);
Draw.rectPlus(0, 0, buttonWidth, buttonHeight, 0xFFFF00, 1, false, 5, 5);
boxImage = new Stamp(Assets.SHOPBUTTON);
textImage.y = (boxImage.height / 2) - (textImage.height / 2);
displayImage = new Graphiclist(boxImage, textImage);
super (x, y, displayImage);
setHitboxTo(boxImage);
Draw.setTarget(tooltipPicture);
Draw.rectPlus(0, 0, 150, 130, 0x000000, 1, true, 1, 0);
Draw.rectPlus(0, 0, 150, 130, 0xFFFF00, 1, false, 5, 0);
tooltipBox = new Stamp(Assets.SHOPBUTTONBORDER);
updateTooltip();
}
public function updateTooltip():void
{
var minDamage:int = 0;
var maxDamage:int = 0;
var damageResist:int = 0;
switch(label)
{
case "Purchase Ammo":
tooltipString[0] = "";
tooltipString[1] = "Purchase more";
tooltipString[2] = "shurikens";
tooltipString[3] = "";
tooltipString[4] = "Shift + Click";
tooltipString[5] = "To buy max";
break;
case "Upgrade Melee Weapon":
minDamage = Globals.meleeDamage[0][Globals.meleeLevel];
maxDamage = Globals.meleeDamage[1][Globals.meleeLevel];
minDamage = minDamage * (1+(Globals.playerMeleeDamage / 100));
maxDamage = maxDamage * (1 + (Globals.playerMeleeDamage / 100));
tooltipString[0] = "Current Level";
tooltipString[1] = "Damage: "
tooltipString[2] = "" + minDamage + " - " + maxDamage;
tooltipString[3] = "Next Level";
if (Globals.meleeLevel < Globals.meleeMaxLevel)
{
minDamage = Globals.meleeDamage[0][Globals.meleeLevel + 1];
maxDamage = Globals.meleeDamage[1][Globals.meleeLevel + 1];
minDamage = minDamage * (1+(Globals.playerMeleeDamage / 100));
maxDamage = maxDamage * (1 + (Globals.playerMeleeDamage / 100));
tooltipString[4] = "Damage: "
tooltipString[5] = "" + minDamage + " - " + maxDamage;
}
else
{
tooltipString[4] = "";
tooltipString[5] = "Max Level";
}
break;
case "Upgrade Ranged Weapon":
minDamage = Globals.rangedDamage[0][Globals.rangedLevel];
maxDamage = Globals.rangedDamage[1][Globals.rangedLevel];
minDamage = minDamage * (1+(Globals.playerRangeDamage / 100));
maxDamage = maxDamage * (1 + (Globals.playerRangeDamage / 100));
tooltipString[0] = "Current Level";
tooltipString[1] = "Damage: "
tooltipString[2] = "" + minDamage + " - " + maxDamage;
tooltipString[3] = "Next Level";
if (Globals.rangedLevel < Globals.rangedMaxLevel)
{
minDamage = Globals.rangedDamage[0][Globals.rangedLevel + 1];
maxDamage = Globals.rangedDamage[1][Globals.rangedLevel + 1];
minDamage = minDamage * (1+(Globals.playerRangeDamage / 100));
maxDamage = maxDamage * (1 + (Globals.playerRangeDamage / 100));
tooltipString[4] = "Damage: "
tooltipString[5] = "" + minDamage + " - " + maxDamage;
}
else
{
tooltipString[4] = "";
tooltipString[5] = "Max Level";
}
break;
case "Upgrade Armor":
damageResist = Globals.armorDefense[Globals.armorLevel];
damageResist = damageResist * (1 + (Globals.playerDefense / 100));
tooltipString[0] = "Current Level";
tooltipString[1] = "Damage Resist: "
tooltipString[2] = "" + damageResist;
tooltipString[3] = "Next Level";
if (Globals.armorLevel < Globals.armorMaxLevel)
{
damageResist = Globals.armorDefense[Globals.armorLevel + 1];
damageResist = damageResist * ( 1 + (Globals.playerDefense / 100));
tooltipString[4] = "Damage Resist: "
tooltipString[5] = "" + damageResist;
}
else
{
tooltipString[4] = "";
tooltipString[5] = "Max Level";
}
break;
}
tooltipText[0] = new Text(tooltipString[0], 0, 10, {width:150, align:"center", size:fontSize, font:"Abscissa"});
tooltipText[0].x = -25;
tooltipText[0].y -= 20;
tooltipText[1] = new Text(tooltipString[1], 0, 30, {width:150, align:"center", size:fontSize, font:"Abscissa"});
tooltipText[1].x = -25;
tooltipText[1].y -= 20;
tooltipText[2] = new Text(tooltipString[2], 0, 45, {width:150, align:"center", size:fontSize, font:"Abscissa"});
tooltipText[2].x = -25;
tooltipText[2].y -= 20;
tooltipText[3] = new Text(tooltipString[3], 0, 65, {width:150, align:"center", size:fontSize, font:"Abscissa"});
tooltipText[3].x = -25;
tooltipText[3].y -= 20;
tooltipText[4] = new Text(tooltipString[4], 0, 85, {width:150, align:"center", size:fontSize, font:"Abscissa"});
tooltipText[4].x = -25;
tooltipText[4].y -= 20;
tooltipText[5] = new Text(tooltipString[5], 0, 100, {width:150, align:"center", size:fontSize, font:"Abscissa"});
tooltipText[5].x = -25;
tooltipText[5].y -= 20;
tooltipBox.x = -25;
tooltipBox.y = -20;
displayImage2 = new Graphiclist(tooltipBox, tooltipText[0], tooltipText[1], tooltipText[2], tooltipText[3], tooltipText[4], tooltipText[5]);
}
public function click(action:String = ""):void
{
//trace(action);
if (action == "Shift" && label == "Purchase Ammo" && callback != null) callback("Shift");
else if (callback != null) callback();
}
override public function update():void
{
super.update();
if (collidePoint(x, y, world.mouseX, world.mouseY))
{
if (Input.mousePressed && !Input.check(Key.SHIFT))
{
shiftClicked = false;
clicked = true;
}
if (Input.mousePressed && Input.check(Key.SHIFT))
{
//trace ("Shifty");
clicked = false;
shiftClicked = true;
}
if (Input.mouseReleased && clicked) click("");
if (Input.mouseReleased && shiftClicked) click("Shift");
hovered = true;
}
else hovered = false;
if (Input.mouseReleased) clicked = false;
if (hovered)
{
updateTooltip();
graphic = displayImage2;
}
else graphic = displayImage;
}
}
When I run it, it displays all but the last line of text (I guess I need a rep of 10 to post a screenshot). For example, the first button displays "Purchase" instead of "Purchase Ammo".
Has anyone ever run into this issue before, and if so how do you get around it?
I've ran into some problems with word wrap. It seems that for Text class the word wrap works correctly when you specify its width and height and then set the word wrap to true and important - resizable to false.
I was just having the exact same problem, try setting the height attribute, that solved for me.