AS3 - Numbers won't display in dynamic text field - actionscript-3

I am writing a program that my brother needs for a project he is working on. It "solves" a password and then shows the number of loops the program goes through before figuring out the solution. I've gotten everything working but the program won't show the numbers in the dynamic text field. It only shows the letters.
Here's the code:
import flash.events.MouseEvent;
button1.addEventListener(MouseEvent.CLICK, solvePassword);
function solvePassword(e:MouseEvent):void
{
var pass:Number = Number(inputText.text);
var attempts:Number = 0;
var i:String = pass.toString();
var passLength:Number = i.length;
var n:Number = 0;
while(n != pass)
{
if(passLength == 1)
{
n += 1;
attempts += 1;
}
if(passLength == 2)
{
n += 1;
if(n < 10) { n = 10 }
attempts += 1;
}
if(passLength == 3)
{
n+=1;
if(n < 100) { n = 100 }
attempts += 1;
}
if(passLength == 4)
{
n+=1;
if(n< 1000) { n = 1000 }
attempts += 1;
}
if(passLength == 5)
{
n+=1;
if(n < 10000) { n = 10000 }
attempts += 1;
}
if(passLength == 6)
{
n+=1;
if(n < 100000) { n = 100000 }
attempts += 1;
}
if(passLength == 7)
{
n+=1;
if(n < 1000000) { n = 1000000 }
attempts += 1;
}
}
i = "Attempts: " + String(attempts);
trace(i);
aBox.text = i;
Any info would help, thank you!

Related

Google Script - get SHA1 script

I cannot for the life of me get the below script to work.
function GetSHA1(input) {
var rawHash = Utilities.computeDigest(Utilities.DigestAlgorithm.SHA_1, input);
var txtHash = '';
for (j = 0; j <rawHash.length; j++) {
var hashVal = rawHash[j];
if (hashVal < 0)
hashVal += 256;
if (hashVal.toString(16).length == 1)
txtHash += "0";
txtHash += hashVal.toString(16);
}
return txtHash;
}
It returns the error
Exception: Argument cannot be null: value GetSHA1 # GetSHA1.gs:2
The script should generate a SHA1 hash of any cells specified in google sheets.
Any help would be appreciated.
Thanks.
This works for me
function GetSHA1(input="0") {
let rawHash = Utilities.computeDigest(Utilities.DigestAlgorithm.SHA_1, input);
let txtHash = '';
for (let j = 0; j <rawHash.length; j++) {
let hashVal = rawHash[j];
if (hashVal < 0)
hashVal += 256;
if (hashVal.toString(16).length == 1)
txtHash += "0";
txtHash += hashVal.toString(16);
}
console.log(txtHash)
return txtHash;
}
This does not
function GetSHA1(input=null) {
let rawHash = Utilities.computeDigest(Utilities.DigestAlgorithm.SHA_1, input);
let txtHash = '';
for (let j = 0; j <rawHash.length; j++) {
let hashVal = rawHash[j];
if (hashVal < 0)
hashVal += 256;
if (hashVal.toString(16).length == 1)
txtHash += "0";
txtHash += hashVal.toString(16);
}
console.log(txtHash)
return txtHash;
}

2 same errors in one script

I have 2 errors:
TypeError: Error #1009: Cannot access a property or method of a null
object reference. at
BlowfishPong_fla::MainTimeline/countTime()[BlowfishPong_fla.MainTimeline::frame79:75]
TypeError: Error #1009: Cannot access a property or method of a null
object reference. at
BlowfishPong_fla::MainTimeline/airScore()[BlowfishPong_fla.MainTimeline::frame79:131]
Look at this code:
timedScore = 0;
var goalScore: int = Math.floor(Math.random() * 101) + 20;
var speedSeconds = 0;
var speedMinutes = 0;
keyNum = 0;
var OxygenTime = 0;
var OxygenMaxTime = 5;
var goalKey: int = Math.floor(Math.random() * 10) + 3;
var ballSpeedXTimed: int = -3;
var ballSpeedYTimed: int = -2;
var cpuPaddleSpeedTimed: int = 3;
stopwatch.play();
oxygenGauge.stop();
var FiftyTimed: Boolean = false;
var plzStopTimed: Boolean = false;
helpContent_Timed.visible = false;
stopwatch.addEventListener(Event.ENTER_FRAME, countTime);
oxygenGauge.addEventListener(Event.ENTER_FRAME, airScore);
goalScore_txt.text = String(goalScore);
timedScore_txt.text = timedScore;
key_txt.text = keyNum + "/" + goalKey;
stage.addEventListener(Event.ENTER_FRAME, loopTimed);
function updateTextFieldsTimed(): void {
goalScore_txt.text = String(goalScore);
timedScore_txt.text = timedScore;
key_txt.text = keyNum + "/" + goalKey;
}
function calculateBallAngleTimed(paddleY: Number, ballY: Number): Number {
var ySpeed: Number = 5 * ((ballY - paddleY) / 25);
return ySpeed;
}
function countTime(e: Event): void {
if (stopwatch.currentFrame == 61) {
speedSeconds++;
if (speedSeconds > 59) {
speedSeconds = 0;
speedtimerSec_txt.text = "0" + speedSeconds;
speedMinutes++;
if (speedMinutes > 10) {
speedtimerMin_txt.text = "" + speedMinutes;
} else {
speedtimerMin_txt.text = "0" + speedMinutes;
}
if (speedMinutes > 59) {
stopwatch.stop();
gotoAndPlay("gameover_Timed");
}
} else {
if (speedSeconds >= 10) {
speedtimerSec_txt.text = "" + speedSeconds;
} else {
speedtimerSec_txt.text = "0" + speedSeconds;
}
}
}
}
function airScore(timedScore): void {
if (timedScore == 50 && FiftyTimed == false) {
character_TiedUp.gotoAndStop(2);
FiftyTimed = true;
oxygenGauge.play();
OxygenTime++;
} else if (timedScore == 150 && FiftyTimed == false) {
character_TiedUp.gotoAndStop(2);
FiftyTimed = true;
oxygenGauge.play();
oxygenGauge.frameRate = 1.5;
OxygenTime++;
} else if (timedScore == 300 && FiftyTimed == false) {
character_TiedUp.gotoAndStop(2);
FiftyTimed = true;
oxygenGauge.play();
OxygenTime++;
} else if (timedScore == 450 && FiftyTimed == false) {
character_TiedUp.gotoAndStop(2);
FiftyTimed = true;
oxygenGauge.play();
OxygenTime++;
} else if (timedScore == 600 && FiftyTimed == false) {
character_TiedUp.gotoAndStop(2);
FiftyTimed = true;
oxygenGauge.play();
OxygenTime++;
}
if (oxygenGauge.currentFrame == 505) {
character_TiedUp.gotoAndStop(3);
oxygenGauge.stop();
oxygenGauge.gotoAndStop(1);
}
}
function loopTimed(event: Event): void {
if (plzStopTimed == false) {
playerPaddle.y = mouseY;
keyPong.x += ballSpeedXTimed;
keyPong.y += ballSpeedYTimed;
//check left and right boundaries
if (keyPong.x <= keyPong.width / 2) {
keyPong.x = keyPong.width / 2;
ballSpeedXTimed *= -1;
keyNum++;
BingTimed.play();
if (goalKey == keyNum) {
key_txt.textColor = 0x00FF00;
}
updateTextFieldsTimed();
} else if (keyPong.x >= stage.stageWidth - keyPong.width / 2) {
keyPong.x = stage.stageWidth - keyPong.width / 2;
ballSpeedXTimed *= -1;
BingTimed.play();
if (keyNum > 0) {
keyNum--;
}
if(goalKey < keyNum) {
key_txt.textColor = 0xFFFFFF;
}
updateTextFieldsTimed();
}
if (keyPong.y <= keyPong.height / 2) {
keyPong.y = keyPong.height / 2;
ballSpeedYTimed *= -1;
gameBounceTimed.play();
} else if (keyPong.y >= stage.stageHeight - keyPong.height / 2) {
keyPong.y = stage.stageHeight - keyPong.height / 2;
ballSpeedYTimed *= -1;
gameBounceTimed.play();
}
if (cpuPaddle.y < keyPong.y - 10) {
cpuPaddle.y += cpuPaddleSpeedTimed;
} else if (cpuPaddle.y > keyPong.y + 10) {
cpuPaddle.y -= cpuPaddleSpeedTimed;
}
if (playerPaddle.y - playerPaddle.height / 4 < 0) {
playerPaddle.y = playerPaddle.height / 4;
} else if (playerPaddle.y + playerPaddle.height / 4 > stage.stageHeight) {
playerPaddle.y = stage.stageHeight - playerPaddle.height / 4;
}
if (playerPaddle.paddle.hitTestObject(keyPong) == true) {
if (ballSpeedXTimed > 0) {
ballSpeedXTimed *= -1;
ballSpeedYTimed = calculateBallAngleTimed(playerPaddle.y, keyPong.y);
timedScore++
gameHitTimed.play();
airScore(timedScore);
if (goalKey == keyNum) {
if (goalScore == timedScore) {
stage.removeEventListener(Event.ENTER_FRAME, loopTimed);
gotoAndStop("gameover_Timed");
return;
}
}
updateTextFieldsTimed();
}
} else if (cpuPaddle.paddle.hitTestObject(keyPong) == true) {
if (ballSpeedXTimed < 0) {
ballSpeedXTimed *= -1;
ballSpeedYTimed = calculateBallAngleTimed(cpuPaddle.y, keyPong.y);
timedScore++;
gameHitTimed.play();
airScore(timedScore);
if (goalKey == keyNum) {
if (goalScore == timedScore) {
stage.removeEventListener(Event.ENTER_FRAME, loopTimed);
gotoAndStop("gameover_Timed");
return;
}
}
updateTextFieldsTimed();
}
}
}
}
Can anyone how to fix these?

I need to slow down my code to see some action on screen

I am creating a slot machine and so far it looks like it is working but the for loop moves so quickly I cannot see the images being replaced.
Any ideas on how can I slow down the for loop or any other way to see the images changing on the screen?
function myFunction() {
var i;
var dicepicture;
var dice = Math.floor(Math.random() * 1000) + 1;
for (i = 0; i < dice; i++) {
var k;
for (k = 1; k < 7; k++) {
document.getElementById("first-picture" + k).src =
'images/empire.jpg';
}
var l;
for (l = 1; l < 7; l++) {
document.getElementById("first-picture" + l).src =
'images/black.jpg';
}
var j;
for (j = 1; j < 7; j++) {
var dicepicture = Math.floor(Math.random() * 11) + 1;
document.getElementById("first-picture" + j).src =
'images/image' + dicepicture + '.jpg';
document.getElementById("vari1").innerHTML = "Value of i is:
" + i;
document.getElementById("vari2").innerHTML = "Value of Dice
is: " + dice;
if (j == 1) {
document.getElementById("first-pic-code").innerHTML =
"Code is: " + dicepicture;
} else if (j == 2) {
document.getElementById("second-pic-code").innerHTML =
"Code is: " + dicepicture;
} else if (j == 3) {
document.getElementById("third-pic-code").innerHTML =
"Code is: " + dicepicture;
} else if (j == 4) {
document.getElementById("forth-pic-code").innerHTML =
"Code is: " + dicepicture;
} else if (j == 5) {
document.getElementById("fifth-pic-code").innerHTML =
"Code is: " + dicepicture;
} else if (j == 6) {
document.getElementById("sixth-pic-code").innerHTML
= "Code is: " + dicepicture;
}
}
}
document.getElementById("vari3").innerHTML = "Value of Dice is: "
+ dice
}
USE
setTimeOut(<timeout_value>) //to delay

Why dont i lag on my first chunk load but i do on my last?

I have a procedural generated game using a perlin map. Iv made it where it only loads the tiles of the area your at, and when you leave that area it deletes them and re draws them according to where you walked too. So theoretically it should only load the tiles of the area you are at. But it seems the further into my map you walk the more it begins to lag. Tho im not sure why since it should never be loading a different amount of blocks.
Here is a link to the game as of now.
http://www.fastswf.com/nzpBar0
These are the functions that are adding and deleting the tiles.
//deletes the tiles on the world.
public function deleteTiles()
{
if (tilesInWorld.length > 0)
{
for (var i:int = 0; i < tilesInWorld.length; i++)
{
worldTiles.removeChild(tilesInWorld.pop());
}
generateTile();
}
}
//generates the tiles on the world
public function generateTile()
{
for (var i:int = X/GlobalCode.MAP_SCALE; i < (X + (800/TILE_SIZE)/GlobalCode.MAP_SCALE); i++)
{
for (var j:int = Y/GlobalCode.MAP_SCALE; j < Y + (600/TILE_SIZE)/GlobalCode.MAP_SCALE; j++)
{
hm = heightmap[i][j];
if (hm >= 0.84)
{
tile = new Water();
}
else if (hm >= 0.8 && hm < 0.84)
{
tile = new Shallow();
}
else if (hm >= 0.7 && hm < 0.8)
{
tile = new Sand();
}
else if (hm >= 0.2 && hm < 0.7)
{
tile = new Tile();
}
else
{
tile = new Stone();
}
tile.width = TILE_SIZE;
tile.height = TILE_SIZE;
worldTiles.x = 0;
worldTiles.y = 0;
tile.x = TILE_SIZE * (i % 800);
tile.y = TILE_SIZE * (j % 600);
tilesInWorld.push(tile);
worldTiles.addChild(tile);
}
}
}
This is where the perlin map and the first tile area is created
public function World(parentMC:MovieClip)
{
TILE_SIZE = GlobalCode.TILE_SIZE;
map_width = GlobalCode.MAP_WIDTH;
map_height = GlobalCode.MAP_HEIGHT;
pmap = new BitmapData(map_width,map_height);
grid_width = new uint(map_width / TILE_SIZE);
grid_height = new uint(map_height / TILE_SIZE);
//map_width = GlobalCode.MAP_WIDTH;
//map_height = GlobalCode.MAP_HEIGHT;
pmap.perlinNoise(map_width,map_height, 6, _seed, true, false, 1, true);
for (var i:uint=0; i < grid_width; i++)
{
heightmap[i] = new Array();
for (var j:uint=0; j < grid_height; j++)
{
heightmap[i][j] = new uint();
}
}
//Divide the map in to a 7x7 grid and take data at each interval
for (i = 0; i < grid_width; i++)
{
for (j = 0; j < grid_height; j++)
{
pixelPoint.x = Math.round((i/grid_width) * pmap.width)+1;
pixelPoint.y = Math.round((j/grid_width) * pmap.height)+1;
heightmap[i][j] = pmap.getPixel(pixelPoint.x,pixelPoint.y);
heightmap[i][j] /= 0xffffff;
if (heightmap[i][j] < darkest_pixel)
{
darkest_pixel = heightmap[i][j];
}
}
}
//Adjust values to a min of 0
for (i = 0; i < grid_width; i++)
{
for (j = 0; j < grid_height; j++)
{
heightmap[i][j] -= darkest_pixel;
if (heightmap[i][j] > brightest_pixel)
{
brightest_pixel = heightmap[i][j];
}
}
}
//Adjust values to highest value of 1
for (i = 0; i < grid_width; i++)
{
for (j = 0; j < grid_height; j++)
{
heightmap[i][j] /= brightest_pixel;
}
}
worldTiles = new Sprite();
parentMC.addChild(worldTiles);
generateTile();
}
this is what creats the scroll rect and the X/Y changes when you walk to the edge of a screen.
public function update(e:Event)
{
world.worldTiles.scrollRect = new Rectangle(X,Y,800,600);
if (canMove == true)
{
MovePlayer();
}
player.update();
PlayerOnTile();
}
And for giggles this is what moves my character and the scroll/rect
protected function MovePlayer()
{
if (goin[0] == 1)
{
player.y -= moveSpeed;
if (player.y <= 0 && (Yloc) > 0)
{
world.Y -= int(600/world.TILE_SIZE)/MAP_SCALE;
world.deleteTiles();
Y -= 600 / MAP_SCALE;
Yloc -= 1;
player.y += 600;
}
}
if (goin[1] == 1)
{
player.y += moveSpeed;
if (player.y >= 600 && (Yloc + 1) < MAP_SCALE)
{
world.Y += int(600/world.TILE_SIZE)/MAP_SCALE;
world.deleteTiles();
Y += 600 / MAP_SCALE;
Yloc += 1;
player.y -= 600;
}
//world.worldTiles.y -= moveSpeed;
}
if (goin[2] == 1)
{
player.x -= moveSpeed;
if (player.x <= 0 && (Xloc) > 0 )
{
world.X -= int(800/world.TILE_SIZE)/MAP_SCALE;
world.deleteTiles();
X -= 800 / MAP_SCALE;
Xloc -= 1;
player.x += 800;
}
//world.worldTiles.x += moveSpeed;
}
if (goin[3] == 1)
{
player.x += moveSpeed;
if (player.x >= 800&& (Xloc + 1) < MAP_SCALE)
{
world.X += int(800/world.TILE_SIZE)/MAP_SCALE;
world.deleteTiles();
X += 800 / MAP_SCALE;
Xloc += 1;
player.x -= 800;
}
//world.worldTiles.x -= moveSpeed;
}
}
It's very simple and basic. You are creating new objects constantly and after a while your app can't get enough memory to create new object and also needs to use big chunk of CPU power to clean up. All this produces lag and is a form of memory leak. You need to reuse object instead of creating new ones. This process is called Object Pooling. Once a graphic is not needed, instead of discarding it and creating a new one next time, you keep it and reuse it next time. That way you don't need additional memory and your app doesn't need more and doesn't need to clean up as much (called garbage collection).
public function deleteTiles()
{
if (tilesInWorld.length)
{
for (var i:int = 0; i < tilesInWorld.length; i++)
{
worldTiles.removeChild(tilesInWorld[i]);//no new object creation when deleting
//tilesInWorld.pop() create a new array internally
}
tilesInWorld.length = 0;//empty array
generateTile();//this should be improved too
}
}
if worldTiles holds only titles then worldTiles.removeChildren() will do it as:
worldTiles.removeChildren();
tilesInWorld.length = 0;
generateTile();
I don't remember how Actionscript handles it, but it might be your for loop in the deleteTiles() function. It only continues until i < tilesInWorld.length, and every iteration of the for loop your i increases by one, and your tilesInWorld decreases by one. This means that you'd only be deleting half of the tiles every time you try to remove all the tiles.
Try using a while loop instead, and see if that fixes it. e.g.
public function deleteTiles()
{
if (tilesInWorld.length > 0)
{
while (tilesInWorld.length > 0)
{
worldTiles.removeChild(tilesInWorld.pop());
}
generateTile();
}
}

Got lost on this code and cant make it work

Im getting an weird error about constructions definitions that i have no idea what means.
Im studing this for a test.I think the problem is on the functions but im not sure.
Its a code from an game (pretty simple one) that you have to move your player from point b to point a without letting the falling asteroids touch you.
Thanks in advance.
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.events.Event;
var up:Boolean;
var down:Boolean;
var left:Boolean;
var right:Boolean;
var vel:Number;
var velInimigo:Number;
var sentidoX:Number;
var sentidoY:Number;
var d:MovieClip;
var txt1:MovieClip;
var chegada:MovieClip;
var player:MovieClip;
var inimigos:Array;
var i:int;
var mc:MovieClip;
function MainTimeline()
{
addFrameScript(0, frame1);
return;
}
function OnKeyDown(event:KeyboardEvent) : void
{
switch(event.keyCode)
{
case Keyboard.W:
{
up = true;
break;
}
case Keyboard.S:
{
down = true;
break;
}
case Keyboard.A:
{
left = true;
break;
}
case Keyboard.D:
{
right = true;
break;
}
default:
{
break;
}
}
return;
}
function OnKeyUp(event:KeyboardEvent) : void
{
switch(event.keyCode)
{
case Keyboard.W:
{
up = false;
break;
}
case Keyboard.S:
{
down = false;
break;
}
case Keyboard.A:
{
left = false;
break;
}
case Keyboard.D:
{
right = false;
break;
}
default:
{
break;
}
}
return;
}
function OnFrame(event:Event) : void
{
var loc3: = null;
var loc4: = null;
if (up)
{
sentidoY = -1;
}
else if (down)
{
sentidoY = 1;
}
else
{
sentidoY = 0;
}
if (left)
{
sentidoX = -1;
}
else if (right)
{
sentidoX = 1;
}
else
{
sentidoX = 0;
}
if (sentidoX == 1 && sentidoY == 0)
{
player.rotation = 0 + 90;
}
else if (sentidoX == 1 && sentidoY == 1)
{
player.rotation = 45 + 90;
}
else if (sentidoX == 0 && sentidoY == 1)
{
player.rotation = 90 + 90;
}
else if (sentidoX == -1 && sentidoY == 1)
{
player.rotation = 135 + 90;
}
else if (sentidoX == -1 && sentidoY == 0)
{
player.rotation = 180 + 90;
}
else if (sentidoX == -1 && sentidoY == -1)
{
player.rotation = 225 + 90;
}
else if (sentidoX == 0 && sentidoY == -1)
{
player.rotation = 270 + 90;
}
else if (sentidoX == 1 && sentidoY == -1)
{
player.rotation = 315 + 90;
}
player.x = player.x + vel * sentidoX;
player.y = player.y + vel * sentidoY;
var loc2: = 0 ;
while (loc2 < inimigos.length)
{
loc3 = inimigos[loc2];
loc3.y = loc3.y + loc3.vel;
loc3.rotation = loc3.rotation + loc3.rotVel;
if (loc3.hitTestObject(player))
{
loc4 = new GameOverMC();
stage.addChild(loc4);
loc4.x = stage.stage.width / 2;
loc4.y = stage.stageHeight / 2;
stage.removeEventListener(Event.ENTERFRAME, OnFrame);
}
if (loc3.y > stage.stageHeight)
{
loc3.y = -loc3.height;
}
loc2++;
}
if (player.hitTestObject(chegada))
{
if (txt1 == null)
{
txt1 = new VitoriaMC();
stage.addChild(txt1);
txt1.x = stage.stage.width / 2;
txt1.y = stage.stageHeight / 2;
}
}
d.x = d.x + d.vel * d.sx;
d.y = d.y + d.vel * d.sy;
if (d.x >= stage.stageWidth - d.width / 2 || d.x <= d.width / 2)
{
d.sx = -d.sx;
}
if (d.y >= stage.stageHeight - d.height / 2 || d.y <= d.height / 2)
{
d.sy = -d.sy;
}
return;
}
function frame1()
{
vel = 5;
velInimigo = 2;
sentidoX = 0;
sentidoY = 0;
d = new DemoMC();
d.vel = 1;
d.sx = 1;
d.sy = 1;
d.x = stage.stageWidth / 2;
d.y = stage.stageHeight / 2;
txt1 = null;
chegada = new ChegadaMC();
player = new PlayerMC();
inimigos = new Array();
stage.addChild(d);
stage.addChild(chegada);
stage.addChild(player);
player.x = player.width / 2;
player.y = stage.stageHeight - player.height / 2;
chegada.x = stage.stageWidth - chegada.width;
chegada.y = 0;
i = 0;
while (i < 60)
{
mc = new AstroMC();
stage.addChild(mc);
inimigos.push(mc);
mc.x = 100 + Math.random() * (600 - mc.width);
mc.y = Math.random() * stage.stageHeight;
mc.rotVel = Math.random() * 10 - 5;
mc.vel = Math.random() * 3 + 1;
var loc1: = i;
var loc2: = i + 1;
loc1.i = loc2;
}
stage.addEventListener(KeyboardEvent.KEYDOWN, OnKeyDown);
stage.addEventListener(KeyboardEvent.KEYUP, OnKeyUp);
stage.addEventListener(Event.ENTERFRAME, OnFrame);
return;
}
It has been awhile since i have played with AS3 but i see a few odd things one being the
loc1.i = loc2;
near the bottom
what is "i" in relation to loc1? and this is in a while loop using i but never increments it so it seems like an infinite loop
again i could be wrong its been quite some time.