SpaceBar Character issues in AS3 - actionscript-3

Im working on a virtual keyboard, but my space key is giving me some issues. When its pressed, it creates the space but it dosent physically show. When I hit a normal letter or number key, the space is now visible with the letter in front of it. The keyboard and space code are nearly identical.
function _keys(event:MouseEvent):void
{
textSelect.setSelection(textSelect.length, textSelect.length);
if(electoff.currentFrame == 2)
{
ajWidth(event);
}
if(terminalinput.currentFrame == 2)
{
TrWidth(null);
}
if (control == "on" && menu.visible == false )
{
if (! shift)
{
textSelect.appendText(letterButtonSmall[event.target.name]);
}
else
{
textSelect.appendText(letterButtonCaps[event.target.name]);
shift = false;
}
savedTxt = textSelect.text;
textSelect.setSelection(textSelect.length, textSelect.length);
if(textSelect.width <= 134.05 && textSelect == inputClips.inputTxt )
{
textSelect.autoSize = TextFieldAutoSize.LEFT;
textSelect.x = 1.2;
}
if (textSelect.width >= 134.05 && textSelect == inputClips.inputTxt)
{
textSelect.autoSize = TextFieldAutoSize.LEFT;
textSelect.x = 1.2 - (textSelect.width-134.05);
}
}
textSelect.setSelection(textSelect.length, textSelect.length);
if(electoff.currentFrame == 2)
{
ajWidth(event);
}
if(terminalinput.currentFrame == 2)
{
TrWidth(null);
}
focuser();
strehrows3();
_fontSize();
}
//spaces
key_space.addEventListener(MouseEvent.CLICK, spaceClickListener);
function spaceClickListener(e:MouseEvent):void
{
strehrows3();
textSelect.setSelection(textSelect.length,textSelect.length);
if (electoff.currentFrame == 2)
{
ajWidth(null);
}
if (terminalinput.currentFrame == 2)
{
TrWidth(null);
}
if (control == "on" && menu.visible == false )
{
if (! shift)
{
textSelect.appendText(" ");
}
else
{
textSelect.appendText(" ");
shift = false;
}
savedTxt = textSelect.text;
textSelect.setSelection(textSelect.length,textSelect.length);
if (textSelect.width <= 134.05 && textSelect == inputClips.inputTxt )
{
textSelect.autoSize = TextFieldAutoSize.LEFT;
textSelect.x = 1.2;
}
if (textSelect.width >= 134.05 && textSelect == inputClips.inputTxt)
{
textSelect.autoSize = TextFieldAutoSize.LEFT;
textSelect.x = 1.2-(textSelect.width-134.05);
}
}
textSelect.setSelection(textSelect.length,textSelect.length);
if (electoff.currentFrame == 2)
{
ajWidth(null);
}
if (terminalinput.currentFrame == 2)
{
TrWidth(null);
}
focuser();
strehrows3();
_fontSize();
}
Now I suspect the autoSize is responsible for my problems but I cant be sure.
Wondering on some creative fixes may work, Ive been contemplating using a blank symbol if there is such a thing.

Related

actionscript break if statement to check next one

I'm making a bot for a simple tic tac toe game. Here is the problem, at botCheck function, when row 0 col 0 and row 0 col 1 is "O", then the "X" appear at the row 0 col 2, and when I tried to make another combination of "O" (ex. row 0 col 0 and row 1 col 1), it should check at different if statement, but my code seems to still check only at the first if statement which will keep tracing ("D") and stuck at that part.
So here is the question, at else{trace("D");} , is there any code that I can replace with to skip the first if statement and go to the second one so it can check for the other combinations of "O" ?
P.S. I've tried to use continue; but it only goes to the next loop and still stuck at the first if statement.
Thanks!
package
{
public class Bot
{
private var n:int = 0;
private var rndm:int;
private var v:int;//vertical
private var h:int;//horizontal
private var moving:Boolean = false;;
private var _main:Main;
public function Bot(main:Main):void
{
_main = main;
}
private function randomNumber(min:Number, max:Number):Number
{
return Math.floor(Math.random()*(1+max-min)+min);
}
public function botMove():void
{
botCheck();
if(n == 0 && moving == false)
{
rndm = randomNumber(0,2);
v = rndm;
rndm = randomNumber(0,2);
h = rndm;
if(_main.squareArray[v][h] == 0)
{
_main.squareArray[v][h] = 1;
_main.xoArray[v][h].push(_main.xo);
moving = true;
fillX();
n = 0;
_main.xo = "O";
}
else
{
botMove();
}
}
if(n == 1 && moving == false)
{
if(_main.squareArray[v][h] == 0)
{
_main.squareArray[v][h] = 1;
_main.xoArray[v][h].push(_main.xo);
moving = true;
fillX();
n = 0;
_main.xo = "O";
}
}
}
private function fillX():void
{
_main.x_ = new X ;
_main.xoContainer.addChild(_main.x_);
_main.x_.x = _main.gameWidth / _main.lebar *(1+(h))+ _main.gameWidth/(_main.lebar*4);
_main.x_.y = _main.gameHeight / _main.panjang * (1+(v))+_main.gameHeight/(_main.panjang*4);
_main.x_.width = _main.square.width / 2;
_main.x_.height = _main.square.height / 2;
}
private function botCheck():void
{
for(var a:int = 0; a<_main.panjang;a++)
{
if(n != 0)
{
break;
}
for(var b:int = 0;b<_main.lebar;b++)
{
if(_main.xoArray[a][b]=="O" && _main.xoArray[a][b+1] == "O")
{
if(b+2 < _main.lebar && _main.xoArray[a][b+2] == 0)
{
n = 1;
v = a;
h = b+2;
moving = false;
break;
}
else if(_main.xoArray[a][b-1] == 0)
{
n = 1;
v = a;
h = b-1;
moving = false;
break;
}
else
{
trace("D");
}
}
else if(_main.xoArray[a][b] == "O" && _main.xoArray[a+1][b] =="O")
{
if(a+2 < _main.panjang && _main.xoArray[a+2][b] == 0)
{
n = 1;
v = a+2;
h = b;
moving = false;
break;
}
else if(a != 0)
{
if(_main.xoArray[a-1][b] == 0)
{
n = 1;
v = a-1;
h = b;
moving = false;
break;
}
}
else
{
}
}
else if(_main.xoArray[a][b]=="O" && _main.xoArray[a+1][b+1] == "O")
{trace("B");
if(a+2 < _main.panjang && b+2 < _main.lebar && _main.xoArray[a+2][b+2] == 0)
{
n = 1;
v = a+2;
h = b+2;
moving = false;
break;
}
else if(a != 0)
{
if(_main.xoArray[a-1][b-1] == 0)
{
n = 1;
v = a-1;
h = b-1;
moving = false;
break;
}
}
else
{
}
}
else if(_main.xoArray[a][b+2]=="O" && _main.xoArray[a+1][b+1] == "O")
{trace("A");
if(a+2 < _main.lebar && _main.xoArray[a+2][b] == 0)
{
n = 1;
v = a+2;
h = b;
moving = false;
break;
}
}
else if(_main.xoArray[a][b] == "O" && _main.xoArray[a+1][b-1] == "O")
{
if(a != 0 && b+1 < _main.lebar && _main.xoArray[a-1][b+1] == 0)
{
n = 1;
v = a-1;
h = b+1;
moving = false;
break;
}
}
else
{
n = 0;
moving = false;
}
}
}
}
}
}
If you would like to check through all the if statements within the for loop, simply remove the "else"s from the other if statements.
Thus, your first if statement is fine, but the next if statement would go from
else if(_main.xoArray[a][b] == "O" && _main.xoArray[a+1][b] =="O")
to simply
if(_main.xoArray[a][b] == "O" && _main.xoArray[a+1][b] =="O")
(just omitting the else).
Repeat that for all of the other outermost "else if"'s in your loop.
Now, I see at the bottom you have a final "else" that sets n to 0 and moving to false. If you make the changes listed above, you may remove that else block, and instead put those statements in the else block (n = 0; moving = false) at the very beginning of the for loop. Therefore, unless any of the if statements are triggered, n will equal 0 and moving will equal false, which is the intended result.
You can also get rid of all of the empty else {} blocks within the for loop; they don't contribute anything anyway. Let me know if you have any questions :)

AS3 Appending text at Caret

Working on a virtual keyboard using flash pro, and im somewhat stuck.
I have a way to control the carat so you can scroll through the text field but I'd like to be able to punch in letters exactly where the carat is using code.
Below is my keyboard controls
// keyboard controls
for (var i=0; i<lettersSmall.length; i++)
{
this["key_" + lettersSmall.charAt(i)].addEventListener(MouseEvent.MOUSE_UP,_keys);
letterButtonSmall["key_" + lettersSmall.charAt(i)] = lettersSmall.charAt(i);
letterButtonCaps["key_" + lettersSmall.charAt(i)] = lettersCaps.charAt(i);
}
function _keys(event:MouseEvent):void
{
curTextPos = textSelect.caretIndex;
strehrows3();
stage.focus = textSelect;
textSelect.setSelection(curTextPos,curTextPos);
if (electoff.currentFrame == 2)
{
ajWidth(event);
}
if (terminalinput.currentFrame == 2)
{
TrWidth(null);
}
if (control == "on" && menu1.visible == false )
{
if (! shift)
{
textSelect.appendText(letterButtonSmall[event.target.name]);
inputMaxWidth = textSelect.width;
}
else
{
textSelect.appendText(letterButtonCaps[event.target.name]);
shift = false;
inputMaxWidth = textSelect.width;
}
savedTxt = textSelect.text;
textSelect.setSelection(textSelect.length,textSelect.length);
if (textSelect.width <= 134.05)
{
//textSelect.autoSize = TextFieldAutoSize.LEFT;
textSelect.x = 1.2;
}
if (textSelect.width >= 134.05)
{
//textSelect.autoSize = TextFieldAutoSize.LEFT;
textSelect.x = 1.2-(textSelect.width-134.05);
}
}
textSelect.setSelection(textSelect.length,textSelect.length);
if (electoff.currentFrame == 2)
{
ajWidth(event);
}
if (terminalinput.currentFrame == 2)
{
TrWidth(null);
}
focuser();
strehrows3();
_fontSize();
textSelect.width = inputMaxWidth;
}
You could do it this way:
var inputStr:String = event.target.name; //This looks like it's value of your key pressed?
var caret:int = textSelect.textField.caretIndex;
var afterCaretText:String = textSelect.text.substr(caret);
var beforeCaretText:String = textSelect.text.substr(0,caret);
textSelect.text = beforeCaretText + inputStr + afterCaretText;
You could do this all in one line without creating the four variables, but I think it's easier to understand what's going on like this.

Adding a single instance of an object to the stage

Good Evening,
I can't seem to get my code to only add one instance of an object to the stage. It seems to add atleast 3 instances of the object to the stage. The objects added is r2, r3 and r4.
stop();
import flash.events.Event;
stage.focus=stage;
var upKeyDown5:Boolean = false;
var rightKeyDown5:Boolean = false;
var downKeyDown5:Boolean = false;
var leftKeyDown5:Boolean = false;
var enterkey:Boolean = false;
var interaction:Boolean = false;
p5.addEventListener(Event.ENTER_FRAME, moveChar5);
stage.addEventListener(KeyboardEvent.KEY_DOWN, checkKeysDown5);
stage.addEventListener(KeyboardEvent.KEY_UP, checkKeysUp5);
Mouse.hide();
function moveChar5(e:Event):void{
if(downKeyDown5 && !upKeyDown5 && !rightKeyDown5 && !leftKeyDown5)
{
p5.gotoAndStop("walk_down");
if(p5.y < 492.75)
p5.y += 6;
}
if(upKeyDown5 && !downKeyDown5 && !rightKeyDown5 && !leftKeyDown5)
{
p5.gotoAndStop("walk_up");
if(p5.y > 202.85)
p5.y -= 6;
}
if(rightKeyDown5 && !upKeyDown5 && !downKeyDown5 && !leftKeyDown5)
{
p5.gotoAndStop("walk_right");
if(p5.x < 871.5)
p5.x += 6;
}
if(leftKeyDown5 && !upKeyDown5 && !rightKeyDown5 && !downKeyDown5)
{
p5.gotoAndStop("walk_left");
if(p5.x > 203.65)
p5.x -= 6;
}
if(enterkey && interaction && p5.hitTestObject(c1)){
if (!(Boolean(stage.getChildByName('r2')))) {
var rat2:r2;
rat2 = new r2();
addChild(rat2);
rat2.y=38;
rat2.x=32;
}
}
if(enterkey && interaction && p5.hitTestObject(c2)){
if (!(Boolean(stage.getChildByName('r3')))) {
var rat3:r3;
rat3 = new r3();
addChild(rat3);
rat3.y=38;
rat3.x=32;
}
}
if(enterkey && interaction && p5.hitTestObject(c3)){
if (!(Boolean(stage.getChildByName('r4')))) {
var rat4:r4;
rat4 = new r4();
addChild(rat4);
rat4.y=38;
rat4.x=32;
}
}
}
function checkKeysDown5(event:KeyboardEvent):void{
if(event.keyCode == 87){
upKeyDown5 = true;
}
if(event.keyCode == 68){
rightKeyDown5 = true;
}
if(event.keyCode == 83){
downKeyDown5 = true;
}
if(event.keyCode == 65){
leftKeyDown5 = true;
}
if (event.keyCode == 13){
enterkey = true;
}
}
function checkKeysUp5(event:KeyboardEvent):void{
if(event.keyCode == 87){
upKeyDown5 = false;
p5.gotoAndStop("still_up");
}
if(event.keyCode == 68){
rightKeyDown5 = false;
p5.gotoAndStop("still_right");
}
if(event.keyCode == 65){
leftKeyDown5 = false;
p5.gotoAndStop("still_left");
}
if(event.keyCode == 83){
downKeyDown5 = false;
p5.gotoAndStop("still_down");
}
if (event.keyCode == 13){
enterkey = false;
}
if(p5.hitTestObject(c1) || p5.hitTestObject(c2) || p5.hitTestObject(c3)){
p5.gotoAndStop("interaction");
interaction = true;
}
else
interaction = false;
}
Thanks in advance
It would be more efficient and cleaner to use a switch statement instead all those if statements (if you only ever want one and only one of those conditions to be met)
You're issue I think has to do with the way you're tracking your rats - eg. using the stage.getChildByName.
Below is an example, plus some commented notes on some of your other potential issues.
switch(true){
case (enterkey && interaction && p5.hitTestObject(c1)):
if (!(Boolean(stage.getChildByName('r2')))) { //your not giving your rat a name so this will always return false, plus you're not adding the rat to the stage but to this class
var rat2:r2;
rat2 = new r2();
addChild(rat2); //use stage.addChild if you want the above check (stage.getChildByName) to work
rat2.y=38;
rat2.x=32;
rat2.name = "r2"; //if you want the above check to work
break; //break out of the switch if you don't want any of the others evaluate since this rat got added
}
//do a case for your other blocks
}
It would be better to not use stage.getChidByName at all, and instead create a method like this:
function checkRatExists(ratClass:Class):Boolean {
var tmp:DisplayObject;
var i:int = numChildren;
while(i--){
tmp = getChildAt(i);
if(tmp is ratClass){
return true;
}
}
return false;
}
Then use that new function instead of stage.getChildByName:
if (!checkRatExists(r2)) //r2 is the class of rat you want to check
As an aside from your issue, it would be much cleaner to create a method/function to do your rat positioning and adding instead of duplicated the code over and over and over...
function addRat(rat:RatCommonBaseClass, ratName:String):void {
addChild(rat);
rat.y=38;
rat.x=32;
rat.name = ratName;
}
//now you can just do this for all your rats:
addRat(new r2(),"r2);
You don't have an else condition for the your statements where you add the objects, so most likely what is happening is that all the statements are satisfying the condition at once. Adding an else statement should prevent this from happening:
if(enterkey && interaction && p5.hitTestObject(c1)){
if (!(Boolean(stage.getChildByName('r2')))) {
var rat2:r2;
rat2 = new r2();
addChild(rat2);
rat2.y=38;
rat2.x=32;
}
}
else if(enterkey && interaction && p5.hitTestObject(c2)){
if (!(Boolean(stage.getChildByName('r3')))) {
var rat3:r3;
rat3 = new r3();
addChild(rat3);
rat3.y=38;
rat3.x=32;
}
}
else if(enterkey && interaction && p5.hitTestObject(c3)){
if (!(Boolean(stage.getChildByName('r4')))) {
var rat4:r4;
rat4 = new r4();
addChild(rat4);
rat4.y=38;
rat4.x=32;
}
}
This way it will first check to see if p5 has hit c1 and only if it hasn't will it check if it has hit c2 and then the same thing for c3.

Sequentially-specific combination of keys held for function?

I'm trying to give my character in a platformer game a movement mechanic in which holding the left key then also the right will cause the character to still move left but at a slower pace (i.e. movementSpeed/2) as if moon-walking (and visa versa):
public var leftKey:Boolean = false;
public var rightKey:Boolean = false;
public var upKey:Boolean = false;
public var leftFlag:Boolean = false;
function ifKeyDown(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.LEFT && rightKey == false)
{
leftKey = true;
if (event.keyCode == Keyboard.LEFT && event.keyCode == Keyboard.RIGHT)
{
leftFlag = true;
trace("leftFlag true");
}
}
if (event.keyCode == Keyboard.RIGHT && leftKey == false)
{
rightKey = true;
}
}
function ifKeyUp(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.LEFT)
{
leftKey = false;
leftFlag = false;
}
if (event.keyCode == Keyboard.RIGHT)
{
rightKey = false;
}
}
public function ifEnterFrame(event:Event):void
{
if (leftKey == true && leftFlag == false)
{
player1_mc.x -= mainSpeed;
trace("L");
}
if (rightKey == true && leftFlag == false)
{
player1_mc.x += mainSpeed;
trace("R");
}
if (leftKey == true && rightKey == true)
{
if (leftFlag == true)
{
player1_mc.x -= mainSpeed/2;
trace("L + R");
}
else
{
player1_mc.x += mainSpeed/2;
trace("R + L");
}
}
My output would look like this:
I hold left key
L
L
L
L
I let go of left key. Then,
I hold right key
R
R
R
R
I let go of right key. Then,
I hold right then also hold left
L
R
R+L
L
R
R+L
I let go of both. Then,
I hold left then also right
L
R
R+L
L
R
R+L
Though I know by my traces that the leftFlag is not being run, I've spent hours trying to figure out why to no avail. :(
I think your problem is this expression:
event.keyCode == Keyboard.LEFT && event.keyCode == Keyboard.RIGHT
Though I am not familiar with actionscript, if it is anything like Java, the keyboard events are called once for each key press. "event" corresponds to only one key, not two different keys, and thus your expression will always return false.
The solution to your problem will probably involve something like this in both the key pressed and released functions.
if (event.keyCode == Keyboard.LEFT)
{
if (leftFlag)
{
//code here
}
if (rightFlag)
{
//code here
}
}
if (event.keyCode == Keyboard.RIGHT)
{
if (leftFlag)
{
//code here
}
if (rightFlag)
{
//code here
}
}
Hope that helps!

AS3 How to shorten this bit of code?

I was working on something and I have this chunky bit of code in there:
if(contents.x>-199 && contents.x<-1) {
mcPosX = 0;
} else if(contents.x>-399 && contents.x<-201) {
mcPosX = -200;
} else if(contents.x>-599 && contents.x<-401) {
mcPosX = -400;
} else if(contents.x>-799 && contents.x<-601) {
mcPosX = -600;
} else if(contents.x>-999 && contents.x<-801) {
mcPosX = -800;
} else if(contents.x>-1199 && contents.x<-1001) {
mcPosX = -1000;
} else if(contents.x>-1399 && contents.x<-1201) {
mcPosX = -1200;
} else if(contents.x>-1599 && contents.x<-1401) {
mcPosX = -1400;
} else if(contents.x>-1799 && contents.x<-1601) {
mcPosX = -1600;
} else if(contents.x>-1999 && contents.x<-1801) {
mcPosX = -1800;
} else if(contents.x>-2199 && contents.x<-2001) {
mcPosX = -2000;
} else if(contents.x>-2399 && contents.x<-2201) {
mcPosX = -2200;
} else if(contents.x>-2599 && contents.x<-2401) {
mcPosX = -2400;
} else if(contents.x>-2799 && contents.x<-2601) {
mcPosX = -2600;
} else if(contents.x>-2999 && contents.x<-2801) {
mcPosX = -2800;
} else {
//mcPosX = contents.x;
}
basically I have a long movieclip (much wider than the stage), it snaps to certain points as you drag it around, depending on which section is shown on the screen.
I feel like this section is really long and I'm trying to improve my AS3. Can this be shortened/improved at all?
Hard to determine exactly what you're trying to do, butI think this might help you on the right direction:
mPos.x -= (Math.floor(contents.x / 200) * 200);