I want to create search feature in as3 - actionscript-3

I wrote this code to my search feature in as3 but it is not good work
because when I click on my button module, the search results are wrong. Please look at my code when I write (123456789123). My module trace is correct and when write (1234567899) my module trace is correct again?
How can I correct this problem?
my code pic is here:
please click to see my code

Make sure your txt3 string is not empty (minimum length is 1). If the txt3 length is zero it will cause problems when you check.
You want your Check function to look something like this :
function check ( evt : MouseEvent) : void
{
trace ("txt3 length is : " + txt3.length); //# if zero you get bad output later
trace ("txt3 text is : " + txt3);
if (txt2.length < 12)
{ trace ("Please Complete Every Space"); }
if ( txt3.length > 0 && txt2.text.search(txt3) >= 0)
{
trace ("correct");
}
else
{
trace ("wrong");
if ( txt3.length < 1 ) { trace ("txt3 is empty String"); }
if ( txt2.text.search(txt3) == -1 ) { trace ("txt2 VS txt3 = match was not found"); }
}
}

Related

which method should i use to create a new keyboard using adobe flash

I'm trying to create a new keyboard somehow, for educational purposes.
I've written this code using actionscript 3.I've created an input text field (named it t1) .when the user presses q button on keyboard(which has an ASCII aquals 81 ) I want the letter b to be printed out on the text field so i've written this code :
stage.addEventListener(KeyboardEvent.KEY_DOWN, pressing);
function pressing(event:KeyboardEvent):void
{
//trace(event.keyCode);
if(event.keyCode==81)
t1.replaceSelectedText("b");
}
the problem was that the method replaceSelectedText prints the tow letters on the screen(q&b) which method can i use instead?
Any help would be appreciated.
When using the replaceSelectedText method, you first need to select the text you want to replace. This can be done with the "setSelection" method. This from the adobe help website:
setSelection(beginIndex:int, endIndex:int):void
"Sets as selected the text designated by the index values of the first and last characters, which are specified with the beginIndex and endIndex parameters."
At the moment, since you don't have any text selected, it appears to just be adding the text "b" as it's replacing nothing. Therefore, you should try first selecting the "q".
Alternatively, you can just use a different method. from the adobe help website:
replaceText(beginIndex:int, endIndex:int, newText:String):void
"Replaces the range of characters that the beginIndex and endIndex parameters specify with the contents of the newText parameter."
This would cut out an extra line of code.
I haven't actually done this myself, so if that doesn't work, here's the link to the adobe help page for Text Fields: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/text/TextField.html
I think that to do what you are looking for ( replace a char when it's typed ), a KeyboardEvent.KEY_DOWN is not enough, because when that event is fired, the text is not yet changed, so any change that you did in its handler to your text field will not cancel the insertion of the current typed char. Also, using KeyboardEvent.KEY_UP ( in addition to KeyboardEvent.KEY_DOWN ) will not resolve the problem, because you can fire n times a KeyboardEvent.KEY_DOWN event with the KeyboardEvent.KEY_UP event fired once !
So, I think that the best event that can do the job is the Event.CHANGE event which is fired every time the text of your text field is changed, so you can do like this :
// is there a char to replace ?
var replace_char:Boolean = false;
// the position of the char that we want to replace
var char_position:int = -1;
var text_input:TextField = new TextField();
text_input.type = 'input';
text_input.border = true;
text_input.addEventListener(Event.CHANGE, onTextChange);
function onTextChange(e:Event):void {
if(replace_char && char_position >= 0){
text_input.replaceText(char_position, char_position + 1, 'b');
replace_char = false;
}
}
addChild(text_input);
stage.addEventListener(KeyboardEvent.KEY_DOWN, _onKeyDown);
function _onKeyDown(e:KeyboardEvent):void {
if(e.keyCode == 81) {
replace_char = true;
char_position = text_input.selectionBeginIndex;
}
}
EDIT :
To use a list of keys and their equivalents, you can use an object to stock your keys like this :
// list of all keys (chars) and their equivalents
var chars:Object = {
81: 'b', // q => b
83: 'v', // s => v
68: 'c' // d => c
// other chars
}
var char_to_replace:int = -1;
// other instructions
function onTextChange(e:Event):void {
if(replace_char && char_position >= 0 && char_to_replace >= 0){
// get the equivalent of the pressed key from chars object using : chars[key_pressed]
text_input.replaceText(char_position, char_position + 1, chars[char_to_replace]);
replace_char = false;
}
}
// other instructions
function _onKeyDown(e:KeyboardEvent):void {
if(chars[e.keyCode]) {
replace_char = true;
// save the last pressed key to get its equivalent, or save this last one directly, to replace it next
char_to_replace = e.keyCode;
char_position = text_input.selectionBeginIndex;
}
}
Hope that can help.

Trace and String Not Working

I'm trying to test some strings but trace outputs nothing. I was using Flash CS6, then I installed Flash CC then everything was fine. But then I created another loop now the same thing. Here's my code:
for (tempNum = lastCharNum; tempNum <= 7; tempNum++) {
trace(tempNum); // this outputs nothing
if (arr[tempNum] != String.fromCharCode(9)) {
firstCharNum = tempNum;
trace(firstCharNum); // this outputs nothing
}
}
I tried:
for (tempNum = lastCharNum; tempNum <= 7; tempNum++) {
txt.text = String(tempNum); // this doesn't make change to the textfield
if (arr[tempNum] != String.fromCharCode(9)) {
firstCharNum = tempNum;
txt.text = String(firstCharNum); // this doesn't make change to the textfield
}
}
Same thing, nothing happens.
Make sure "Omit Trace Statements" is not checked in the the publish settings. If its not checked, try File>Publish and Control>Test Movie>In Flash Professional to see if you get different results.

Checking for same values using if statement in actionscript?

I'm working on a match-3 style puzzle game using Flixel, and so I'm working on checking each row and column to see if there is a match at any given time. However, I have 6 different pieces (as of right now) that are active, and each piece is identified by an integer. Given that, I can check, for each and every single piece, by doing something like this:
public function matchingCheck():void
{
if (piecesArray[0][1] == 1 && piecesArray[1][1] == 1 && piecesArray[2][1] == 1) {
FlxG.log("Yay!");
}
}
However, this is rather unwieldy and would basically cause way too much repetition for my liking.
At the very least, I would like to be able to check if the values in these arrays are equal to one another, without having to specify which value it is. At the very best, I'd love to be able to check an entire row for three (or more) adjacent pieces, but I will settle for doing that part manually.
Thanks for your help!
EDIT: Nevermind, my edit didn't work. It was just checking if piecesArray[2][1] == 1, which makes me a sad panda.
EDIT 2: I've selected the correct answer below - it's not exactly what I used, but it definitely got me started. Thanks Apocalyptic0n3!
You could cut down on that code a little bit by using another function
private function checkValid( arrayOfItemsToCheck:Array, value:* ):Boolean {
for ( var i:Number = 0; i < arrayOfItemsToCheck.length; i++ ) {
if ( arrayOfItemsToCheck[i] != value ) {
return false;
}
}
return true;
}
Then you just do this in your if statement:
if ( checkValid( [ piecesArray[0][1], piecesArray[1][1], piecesArray[2][1] ], 1 ) ) {
FlxG.log("Yay!");
}
That does assume all items need to be equal to 1, though. It's still a lot of code, but it cuts out one set of "= 1 &&" for each check.
How about something like this which would tell you both if a match existed and what match it was:
public function checkForMatch():void{
var rows:int = piecesArray.length;
for(var i:int=0; i<rows; i++){
var match:int = checkRow(piecesArray[i]);
if(match > -1) {
FlxG.log("Yay you matched " + match);
}
}
}
private function ckeckRow(row:Array):int{
if(row[0] == row[1] == row[2]){
return row[0];
}
return -1;
}

As3 strange If statement evaluation behaviour

I am writing a small game for a bit of fun. It's a simple turn based battle game kind of like Pokemon style battles but with stupid/joke characters.
I have some stuff on the main timeline for user interface but everything else is handled in external actionscript files. I have a class for a battle which has some battle related methods like attack etc and I also have characters as their own classes which inherit from an interface.
In my user interface there are some buttons and when a battle commences I have an event listener on my attack button which executes the following code each time:
public function attack(attacker:Character, defender:Character):void
{
var defenderHp:int;
var attackPower:int;
var postAttackHp:int;
defenderHp = defender.getHP();
attackPower = attacker.getAttack();
if (! passiveBlock(defender))
{
if (! criticalStrike(attacker))
{
trace("defender hp trace: " + (defenderHp - attackPower));
postAttackHp = (defenderHp - attackPower);
}
else
{
trace("defender hp trace: " + Math.floor((defenderHp - (attackPower*1.5))));
postAttackHp = Math.floor((defenderHp - (attackPower*1.5)));
displayText = attacker.getName() + " landed a critical strike!";
}
if (! postAttackHp > 0)
{
gameIsOver(attacker, defender);
}
else
{
defender.setHP(postAttackHp);
}
}
else
{
displayText = defender.getName() + " blocked the attack!";
}
}
The code gets called by the button fine every time, the problem lies in the if (! postAttackHp > 0) condition. For some reason only occasionally does the game over method get called when the defender's HP goes below 0. I am tracing the postAttackHp variable out each time and I can see every time that it is below 0 but the if statement more often than not skips down to the else section.
What is strange is that sometimes it works properly and sometimes it does not but it is more frequently not working.
Is there something fundamentally stupid about the way this is set up?
Thanks in advance for your help :)
I don't have Flash installed on this machine, or else I would try to see if this is the issue, but my first guess would be that
! postAttackHp > 0
is evaluating as (! postAttackHp) > 0. In this case, it would only be true if postAttackHp were == 0. Anything else, even a negative number, would evaluate to False, since ! -10 = False. False > 0 is False, since typecasting False would leave it as 0.
Try either,
if (! (postAttackHp > 0))
or
if (postAttackHp <= 0)

What does this line of Actionscript do?

I'm looking at the as3delaunay library and most of the code is clear to me. This part is not, however (note the line that I put preceded with an arrow):
public function circles():Vector.<Circle>
{
var circles:Vector.<Circle> = new Vector.<Circle>();
for each (var site:Site in _sites)
{
var radius:Number = 0;
var nearestEdge:Edge = site.nearestEdge();
=======>> !nearestEdge.isPartOfConvexHull() && (radius = nearestEdge.sitesDistance() * 0.5);
circles.push(new Circle(site.x, site.y, radius));
}
return circles;
}
For reference, isPartOfConvexHull() is found in Edge.as and looks like this:
internal function isPartOfConvexHull():Boolean
{
return (_leftVertex == null || _rightVertex == null);
}
What does !nearestEdge.isPartOfConvexHull() do? Does that mean that the radius = nearestEdge.sitesDistance() * 0.5 only executes if false is returned from the call to isPartOfConvexHull()? Does that stop execution of any other code?
It is equivalent to:
if (!nearestEdge.isPartOfConvexHull()) {
radius = nearestEdge.sitesDistance() * 0.5;
}
In the following line:
var b:Boolean = expression1 && expression2;
expression2 will not be evaluated if expression1 is false because we already know the final result: b = false.
Now in the following line:
expression1 && expression2;
The same thing happens except the fact that we are not assigning the result to a variable.
And this is exactly what happens in the line you are asking about where !nearestEdge.isPartOfConvexHull() is the first expression and (radius = nearestEdge.sitesDistance() * 0.5) is the second expression.
To extends #sch answer with some explanations (I didn't knew if editing answer to almost double it was ok).
This is based on lazy execution of the interpreter. If (!nearestEdge.isPartOfConvexHull()) is False then there's no need to execute the second part of the AND statement to know it'll be False, then it's left unexecuted. If it's true the evaluation of the complete statement is needed (and then done) to tell wether or not this boolean is True. So this is equivalent to an if statement.
TMHO this is bad code since it's to much condensed and hard to understand.