...

View Full Version : Detect value in textfield and then perform action



CaptainB
04-16-2009, 12:48 PM
Hi,

This is my situation:

I have a textbox. If the correct value is written in it, it jumps to a certain frame in my flash movie (succes). If not, it jumps to another (fail).

How would I archieve that? :confused:

gnomeontherun
04-19-2009, 09:46 PM
Interesting concept, like a password protection?

You would need to have a function that compares the input text against the correct value. Then just have a button call this function when the user is ready to test their entry. This is something I wrote in 3 mins, might not be right but gives you a start.


function checkText() {
inputText = _root.inputTextField.value;
if (inputText == "password")
{
gotoAndStop(2);
}
else
{
gotoAndStop(3);
}
}

CaptainB
04-20-2009, 08:57 PM
Jeremy,
More like a simple math-game - but interesting idea with the password, I guess you could use it for that too.

Thank you for your suggestion, I'll try it off ASAP and report back here !

CaptainB
05-07-2009, 12:48 PM
Jeremy, it diden't work unfortunately. However, I just found this: http://www.webwasp.co.uk/tutorials/b08-interactive/index.php :)

scottyp2
05-08-2009, 12:11 AM
I see why Jeremy's code doesn't work. There's nothing to tell flash when the user is done to have flash do something. Without a button. There is a way to set the focus to a text field, something like
stage.focus = myTextField

Another option would be to create an onEnterFrame function and when it's running you could create a condition

function typeSomething() {
this.onEnterFrame = function() {
if (stage.focus != myTextField) {
//do something
}else {
this.onEnterFrame = null;
return;
//do something else
}
}
}

But best to do with a button so you have more control.

CaptainB
05-23-2009, 05:36 PM
Hmm, I still haven't made this work.

I have a button: button_cubmit
And a textfield: Resultat

This is my code added to my 'actions' layer:


button_submit.addEventListener(MouseEvent.CLICK, subClick);

function subClick(event:MouseEvent):void{
myPass = Resultat.text;
if(myPass == "password"){
gotoAndStop("rigtigt");
} else {
gotoAndStop("forkert");
}



My result:
1084: "Syntax error: Expecting rightbrace before end of program".. And my flash movie just keeps changing through all the scenes.

bigtiger
05-24-2009, 06:49 PM
Hi,

I did a simple test on flash 8.
Hmmm, it may or may not help.

Setting frames to 20 and add two keyframes to 5 and 10.
Place a TextField (name: txtpass) on the stage and add this as2 code in frame.



function do_checkpass(cf:TextField)
{
var targetFrame:Number;
var passkey:String = 'password';

if (cf.text != passkey)
targetFrame = 5;
else
targetFrame = 10;
cf._parent.gotoAndStop(targetFrame);
}

txtpass.onChanged = function(cf:TextField)
{
do_checkpass(cf);
}

stop();

CaptainB
05-24-2009, 09:07 PM
Hi there,

I'm a bit unsure on where to add the code. I tried to add it to my button element, however, that doesn't work. The flash movie just keeps looping through all the frames. Also, I get some errors (see pic). I'm probably doing something wrong. By the way, my document is AS3 :(

bigtiger
05-24-2009, 10:08 PM
Hi,

It has big change between AS2 and AS3.
That's why you got a lot of error messages on a as3 document.
Actually I never worked with As3 before.
But the basic logic should be the same.

Just did a few search on adobe.com and found a example that show how change event of textfield work. It is simliar to as2 onChange event, but with as3 syntax.

Can you operate MoiveClip with as3 ?
If you do, then this code should be the only one you need to build your stuffs.

http://help.adobe.com/en_US/AS3LCR/Flash_10.0/index.html

Code example from adobe liveDoc:

In the following example, the text that the user enters (user input) is immediately copied (echoed) into another text field with a different text format.
Two text fields are created, one for the user input and the other (headingTextField) for the copy of the user input. A TextFormat object is also created and the default text format is assigned to the headingTextField text field. When the content of the text field is changed, the changeHandler() method is invoked, which assigns the text in the inputTextField text field to the headingTextField text field. (If the method was called for the TextEvent.TEXT_INPUT event instead of the Event.CHANGE event, the content of the user input is copied only after the user has entered more text.)



package {
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFieldType;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
import flash.events.Event;

import flash.events.TextEvent;
public class TextField_Event_changeExample extends Sprite {
private var inputTextField:TextField = new TextField();
private var headingTextField:TextField = new TextField();
private var newFormat:TextFormat = new TextFormat();

public function TextField_Event_changeExample() {
headingTextField.x = 10;
headingTextField.y = 10;
headingTextField.height = 30;
headingTextField.width = 400;
headingTextField.background = true;
headingTextField.backgroundColor = 0xF5F5DC;
headingTextField.selectable = false;

inputTextField.x = 10;
inputTextField.y = 70;
inputTextField.height = 20;
inputTextField.width = 230;
inputTextField.background = true;
inputTextField.border = true;
inputTextField.maxChars = 40;
inputTextField.wordWrap = true;
inputTextField.type = TextFieldType.INPUT;
inputTextField.addEventListener(Event.CHANGE, changeHandler);
newFormat.bold = true;
newFormat.size = 18;
newFormat.color = 0xFF0000;
newFormat.align = TextFormatAlign.CENTER;
headingTextField.defaultTextFormat = newFormat;
this.addChild(inputTextField);
this.addChild(headingTextField);
}
private function changeHandler(e:Event):void {
headingTextField.text = inputTextField.text;
}
}
}

CaptainB
05-25-2009, 12:19 AM
I had a look at the script. Most of it is actually pure magic as I've never worked with AS before :(

However, I THINK I managed to strip it down to what I need - but what's inside the 'changeHandler is by no means correct - I really need some more assistance here if possible!

But Bigtiger, thank you so much for your help so far!



function Check_Value() {
Resultat.addEventListener(Event.CHANGE, changeHandler);
}

function changeHandler(e:Event):void {

var targetFrame:Number;
var passkey:String = 'password';

if (Resultat != passkey)
targetFrame = 5;
else
targetFrame = 10;
gotoAndStop(targetFrame);
}

bigtiger
05-26-2009, 07:08 PM
Hi, CaptainB

I worked with AS3 and a few pages of code example and tutorials; finally I got the following code.


Place the following code in a text file and save and name it as Auth.as.


package {
import flash.display.Sprite;
import flash.display.MovieClip;
import flash.text.TextField;
import flash.events.Event;
import flash.events.TextEvent;

public class Auth extends Sprite {
private var inputTextField:TextField = new TextField();
provate var m_mc:MovieClip = null;

public function Auth(mc:MovieClip) {
inputTextField.x = 10;
inputTextField.y = 70;
inputTextField.height = 20;
inputTextField.width = 230;
inputTextField.background = true;
inputTextField.border = true;
inputTextField.maxChars = 40;
inputTextField.wordWrap = true;
inputTextField.type = TextFieldType.INPUT;
inputTextField.addEventListener(Event.CHANGE, changeHandler);
this.addChild(inputTextField);

this.m_mc = mc;
}

private function changeHandler(e:Event):void {
// trace('changeHandler cb');

if (this.inputTextField.text != 'password') {
this.m_mc.gotoAndStop(5);
} else {
this.m_mc.gotoAndStop(10);
}
}
}
}


Create a new flash document and save it to the same location as file, Auth.as.

Create a new MovieClip symbol instance on the stage by converting from a drawing or create a new one.
Double-click the MovieClip instance on the stage to enter Edit-inplace mode.
Create new keyframes at frame 5 and 10 in the Timeline of the MovieClip instance, and place some identify labels on it.
Enter the following code in frame1 in the Timeline of the MovieClip instance.


var auth:Auth = new Auth(this);

stop();




It is not a good and best way to code. But it seems to be work.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum