AS3 - Making a True/False Quiz with ComboBoxes - actionscript-3

So I've been working on a larger project for a while and am having a little confusion with a quiz I am intergrating into it.
I would like to use ComboBoxes to create a true/false quiz. I will be using a switch structure to track the answer of the end-user. I already now how to use a switch structure for ComboBoxes, however, I am unsure as to how I can:
1.) determine if all labels in the ComboBoxes are correct, and;
2.) how I can make my structure evn work. I am having troubles right now.
Please see code below. You will see that it is a logical error. I am unsure of what the issue is, so your help is appreciated again!
Thanks!
-Zero;
import fl.controls.ComboBox;
import flash.events.MouseEvent;
F21next_btn.visible=false;
firstTF_cb.addItem({label: "True"});
firstTF_cb.addItem({label: "False"});
secondTF_cb.addItem({label: "True"});
secondTF_cb.addItem({label: "False"});
thirdTF_cb.addItem({label: "True"});
thirdTF_cb.addItem({label: "False"});
fourthTF_cb.addItem({label: "True"});
fourthTF_cb.addItem({label: "False"});
fifthTF_cb.addItem({label: "True"});
fifthTF_cb.addItem({label: "False"});
F21check_btn.addEventListener(MouseEvent.CLICK, F21checkAnswers);
F21next_btn.addEventListener(MouseEvent.CLICK, F21goToFrameTwentytwo);
var firstCB:ComboBox;
var secondCB:ComboBox;
var thirdCB:ComboBox;
var fourthCB:ComboBox;
var fifthCB:ComboBox;
firstCB=firstTF_cb;
secondCB=secondTF_cb;
thirdCB=thirdTF_cb;
fourthCB=fourthTF_cb;
fifthCB=fifthTF_cb;
function F21goToFrameTwentytwo(event:MouseEvent):void{
gotoAndStop(22);
}
//HOW DO U CHECK IF A COMBO BOX ITEM IS TRUE/FALSE AND SELECTED
function F21checkAnswers(event:MouseEvent):void
{
switch(firstCB)
{
case "True":
{
firstQ_txt.textColor=0x00FF00;
break;
}
case "False":
{
firstQ_txt.textColor=0xFF0000;
break;
}
}
switch(secondCB)
{
case "True":
{
secondQ_txt.textColor=0x00FF00;
break;
}
case "False":
{
secondQ_txt.textColor=0xFF0000;
break;
}
}
switch(thirdCB)
{
case "True":
{
thirdQ_txt.textColor=0x00FF00;
break;
}
case "False":
{
thirdQ_txt.textColor=0x00FF00;
break;
}
}
switch(fourthCB)
{
case "True":
{
fourthQ_txt.textColor=0x00FF00;
break;
}
case "False":
{
fourthQ_txt.textColor=0xFF0000;
break;
}
}
switch(fifthCB)
{
case "True":
{
fifthQ_txt.textColor=0x00FF00;
break;
}
case "False":
{
fifthQ_txt.textColor=0xFF0000;
break;
}
}
}

The selectedLabel property of ComboBox gives the currently selected string of the combo box.
switch(firstCB.selectedLabel)
{
case "True":
{
firstQ_txt.textColor = 0x00FF00;
break;
}
case "False":
{
firstQ_txt.textColor = 0xFF0000;
break;
}
}

Related

VisJs How to import json data without creating duplicate edges?

How can I import new json data (gephiJSON) without duplicating edges in my existing data?
My code is as simple as that:
var parsed = vis.network.gephiParser.parseGephi(gephiJSON, parserOptions);
nodes.update(parsed.nodes);
edges.update(parsed.edges);
I am afraid I did not find any option or function to avoid/check duplicates.
Appreciate any help.
*I have no control over the imported data to avoid duplicates.
I use nodes.update method only to update existing network. To create new ones I use nodes.add. So, it's important to check existing data in your network before trying to add. I used a double loop routine plus a splice method for the JSON to delete duplicated nodes and edges. My code was like this (please be patient with me, I'm new in JavaScript) :
function recibedatos() {
if (http_request.readyState == 4) {
if (http_request.status == 200) {
fixedResponse = http_request.responseText;
jsonObj = JSON.parse(fixedResponse);
try {
for (var i=0;i<jsonObj.nodos.length;i++) {
for (var j=0;j<nodes.length;j++) {
if (jsonObj.nodos[i].id==nodes.get()[j].id) {
jsonObj.nodos.splice(i,1);
i-=1;
break;
}
}
}
nodes.add(jsonObj.nodos);
}
catch (err) {
alert(err);
}
try {
for (var i=0;i<jsonObj.vinculos.length;i++) {
for (var j=0;j<edges.length;j++) {
if (jsonObj.vinculos[i].to==edges.get()[j].to && jsonObj.vinculos[i].from==edges.get()[j].from && jsonObj.vinculos[i].label==edges.get()[j].label) {
jsonObj.vinculos.splice(i,1);
i-=1;
break;
}
}
}
edges.add(jsonObj.vinculos);
}
catch (err) {
alert(err);
}
} else {
alert("Ocurrio un problema con la URL.");
}
fixedResponse=null;
jsonObj=null;
}
}

Get cocos2d-x keyboard info on android

I simply insert following codes in a clean base(at the final of HelloWorld::init()) produced by cocos new:
auto kl = EventListenerKeyboard::create();
kl->onKeyPressed = [](EventKeyboard::KeyCode keyCode, Event* event) {
CCLOG("%s> keyCode=%d",__FUNCTION__,keyCode);
};
auto ed = Director::getInstance()->getEventDispatcher();
ed->addEventListenerWithSceneGraphPriority(kl,this);
On windows, it worked well. But nothing happened on android.
Log with value of keyCode should be left in logcat, I think.
Do I miss anything?
Approach One:
If you just need to handle keyboard as key-event, It's as easy as these below lines of code:
HelloWorld::init()
{
...
auto keyboardListener = EventListenerKeyboard::create();
keyboardListener->onKeyPressed = [](EventKeyboard::KeyCode keyCode, Event* event)
{
switch (keyCode)
{
case EventKeyboard::KeyCode::KEY_UP_ARROW: /*Jump maybe*/ break;
case EventKeyboard::KeyCode::KEY_DOWN_ARROW: /*Crouch maybe*/ break;
case EventKeyboard::KeyCode::KEY_RIGHT_ARROW: /*Move Right maybe*/ break;
case EventKeyboard::KeyCode::KEY_LEFT_ARROW: /*Move Left maybe*/ break;
}
};
_eventDispatcher->addEventListenerWithSceneGraphPriority(keyboardListener, this);
...
return true;
}
I think it's clear enough not to need any extra description.
Approach Two: if you need an input box that user/player can enter string with keyboard and you get what is entered, I recommend to use TextField which is available in cocos2d v3 ( and with some difficulty in v2) and has a full functionality. You can create and initial one of them as:
auto textField = cocos2d::ui::TextField::create("hint: enter here","Arial" , 30);
textField->setTextHorizontalAlignment(cocos2d::TextHAlignment::CENTER);
textField->setTextVerticalAlignment(cocos2d::TextVAlignment::CENTER);
textField->setColor(Color3B(100,100,100));
textField->setMaxLength(10);
textField->setMaxLengthEnabled(true);
textField->setTouchAreaEnabled(true);
textField->setTouchSize(Size(200,400));
textField->setPosition(...);
textField->addEventListener(CC_CALLBACK_2(HelloWorld::textFieldEvent, this));
this->addChild(textField, 10);
You can get entered data any time with std::string enteredData= textField->getString();
You can also do something when user entering text with two event as :
void HelloWorld::textFieldEvent(Ref *pSender, cocos2d::ui::TextField::EventType type)
{
switch (type)
{
case cocos2d::ui::TextField::EventType::ATTACH_WITH_IME:
{
textField->setColor(Color3B::BLACK);
// or whatever elese
break;
}
case cocos2d::ui::TextField::EventType::DETACH_WITH_IME:
{
textField->setColor(Color3B(100,100,100));
// or whatever elese
break;
}
}
}
Enjoy !

An if/else issue in ActionScript 3

For a project I'm working on, I'm making a simple text-based adventure. At one point, the user sees two keys and a door nearby. I'm trying to make it so that if the user enters the door without both keys, it opens up the frame where it states that they will need both to open it (Frame 11).
The problem is, whether or not the user grabs them, it automatically directs them through the door as if they have opened it (Frame 12).
Here's my total coding. Don't mind commented portions, that's just me trying out some different approaches.
import flash.events.MouseEvent;
import flash.net.navigateToURL;
import flash.net.URLRequest;
import flash.net.URLRequestMethod;
var torchb: Boolean = false;
var redkeyb: Boolean = true;
var bluekeyb: Boolean = true;
stop();
StartButtonInst.addEventListener(MouseEvent.CLICK, Page2);
function Page2(event:MouseEvent):void
{
gotoAndStop(2);
LeftButtonInst.addEventListener(MouseEvent.CLICK, Page3);
RightButtonInst.addEventListener(MouseEvent.CLICK, Page4);
}
function Page3(event:MouseEvent):void
{
gotoAndStop(3);
GoBack1Inst.addEventListener(MouseEvent.CLICK, Page2);
}
function Page4(event:MouseEvent):void
{
gotoAndStop(4);
WalkOffInst.addEventListener(MouseEvent.CLICK, Page5);
}
//function torch (e:MouseEvent){
/*if(torchb == true){
this.removeChild(GrabTorchInst);
this.gotoAndStop(7);
}
else{
this.gotoAndStop(6);
} */
/*if(!torchb) {
torchb = true;
this.removeChild(GrabTorchInst);
//this.gotoAndStop(7);
}
else {
torchb = false;
this.gotoAndStop(6);
}*/
//GrabTorchInst.addEventListener(MouseEvent.CLICK, torch);
//This event should change the boolean if the torch is picked up
//EnterCaveInst.addEventListener(MouseEvent.CLICK, cave);
//This event should change the frame to the cave or not depending what the torch bool is.
function torch (e:MouseEvent) {
torchb = torchb==true?false:true;
this.removeChild(GrabTorchInst);
}
function cave(e:MouseEvent) {
if(torchb) {
this.gotoAndStop(7);
}
else {
this.gotoAndStop(6);
}
}
function Page5(event:MouseEvent):void
{
trace(torchb);
gotoAndStop(5);
GrabTorchInst.addEventListener(MouseEvent.CLICK, torch);
EnterCaveInst.addEventListener(MouseEvent.CLICK, cave);
//GrabTorchInst.addEventListener(MouseEvent.CLICK, Page5);
EnterCaveInst.addEventListener(MouseEvent.CLICK, Page7);
/*if (GrabTorchInst = true)
{
this.gotoAndStop(7);
} */
}
/*function torch (e:MouseEvent){
if(!torchb) {
torchb = true;
this.removeChild(GrabTorchInst);
this.gotoAndStop(7);
}
else {
torchb = false;
this.gotoAndStop(6);
}
}*/
function Page6(event:MouseEvent):void
{
gotoAndStop(6);
GoBack2Inst.addEventListener(MouseEvent.CLICK, Page5)
}
function Page7(event:MouseEvent):void
{
gotoAndStop(7);
LightTorchInst.addEventListener(MouseEvent.CLICK, Page8)
AssassinateInst.addEventListener(MouseEvent.CLICK, Page10)
}
function Page8(Event:MouseEvent):void
{
gotoAndStop(8);
AssassinateInst.addEventListener(MouseEvent.CLICK, Page10)
RunAwayInst.addEventListener(MouseEvent.CLICK, Page9)
}
function Page9(Event:MouseEvent):void
{
gotoAndStop(9);
GoBack3Inst.addEventListener(MouseEvent.CLICK, Page7)
}
/*function redkey (e:MouseEvent){
if(!redkeyb) {
redkeyb = true;
this.removeChild(RedKeyInst);
//this.gotoAndStop(12);
}
else {
redkeyb = false;
this.gotoAndStop(11);
}
} */
function redkey (e:MouseEvent) {
redkeyb = redkeyb==true?false:true;
this.removeChild(RedKeyInst);
}
function bluekey (e:MouseEvent) {
bluekeyb = bluekeyb==true?false:true;
this.removeChild(BlueKeyInst);
}
function door(e:MouseEvent) {
if(redkey==true && bluekey==true){
(12)
this.gotoAndStop(12);
}
else {
this.gotoAndStop(11);
}
}
/*function bluekey (e:MouseEvent){
if(!bluekeyb) {
bluekeyb = true;
this.removeChild(BlueKeyInst);
//this.gotoAndStop(12);
}
else {
bluekeyb = false;
this.gotoAndStop(11);
}
} */
if(redkey==true&&bluekey==true){
(12)
}
function Page10(Event:MouseEvent):void
{
gotoAndStop(10);
RedKeyInst.addEventListener(MouseEvent.CLICK, redkey)
BlueKeyInst.addEventListener(MouseEvent.CLICK, bluekey)
DoorInst.addEventListener(MouseEvent.CLICK, door)
DoorInst.addEventListener(MouseEvent.CLICK, Page12)
}
function Page11(Event:MouseEvent):void
{
gotoAndStop(11)
GoBack4Inst.addEventListener(MouseEvent.CLICK, Page10)
}
function Page12(Event:MouseEvent):void
{
gotoAndStop(12)
GiveJakeAnAInst.addEventListener(MouseEvent.CLICK, Page13)
}
function Page13(Event:MouseEvent):void
{
gotoAndStop(13)
}
I'd really appreciate some assistance on this.
Don't know if this is what you want but you can try
if(torchb == true){
this.removeChild(GrabTorchInst);
this.gotoAndStop(7);
}
else{
this.gotoAndStop(6);
}
So if torchb is true, they would be sent to 7th frame
if it isn't, they would be sent to sixth frame.

send keyCode without key event?

I got this function (in the document class)
public function kyz(event:KeyboardEvent):void{
trace(event.keyCode);
switch (event.keyCode){
case 65:{
if (ppm.currentFrame<200 || ppm.currentFrame>300) {
ppm.gotoAndStop(301);
ssm.gotoAndStop(302);
llm.gotoAndStop(302);
mmm.gotoAndStop(302);
myTimer.stop();
pd.play();
}else {
pd.play();
ppm.gotoAndPlay(10);
tlrnc-=10;
}
break;
}
case 68:{
if (ssm.currentFrame<200 || ssm.currentFrame>300) {
ssm.gotoAndStop(301);
ppm.gotoAndStop(302);
llm.gotoAndStop(302);
mmm.gotoAndStop(302);
myTimer.stop();
mTimer.stop();
sd.play();
}else {
sd.play();
ssm.gotoAndPlay(10);
tlrnc-=10;
}
break;
}
case 74:{
if (llm.currentFrame<200 || llm.currentFrame>300) {
llm.gotoAndStop(301);
ppm.gotoAndStop(302);
ssm.gotoAndStop(302);
mmm.gotoAndStop(302);
myTimer.stop();
mTimer.stop();
ld.play();
}else {
ld.play();
llm.gotoAndPlay(10);
tlrnc-=10;
}
break;
}
case 76:{
if (mmm.currentFrame<200 || mmm.currentFrame>300) {
mmm.gotoAndStop(301);
ppm.gotoAndStop(302);
ssm.gotoAndStop(302);
llm.gotoAndStop(302);
myTimer.stop();
mTimer.stop();
md.play();
}else {
md.play();
mmm.gotoAndPlay(10);
tlrnc-=10;
}
break;
}
}
}
that receives key event's and do stuff. now I'm trying to pass they keyCode from another function (to avoid changing the whole code for adding click functionality) got any suggestions?
you could dispatch a KeyboardEvent
stage.dispatchEvent(
new KeyboardEvent(KeyboardEvent.KEY_DOWN, true, false, myCharCode, myKeyCode));
just add the needed keyCode as a parameter
You cannot send any Keyboard or Mouse event without interactivity with user. You can handle this in another private function:
private function keyDownHandler(event : KeyboardEvent) : void {
this.handleEvent(event.keyCode);
}
private function handleEvent(keyCode : uint) : void {
//some actions
}
And when you need to make specific actions, you can just call handleEvent function without user side interactivity.

switch inside another switch actionscript 3

i have this lines of code that are very long and i want to know if this codes can be converted from this
if(BtnInteracted == "New"){
if(sample_1.SaveSlotNumber == "1"){
if(){
}
else{
}
}
else if(sample_1.SaveSlotNumber == "2"){
if(){
}
else{
}
}
else if(sample_1.SaveSlotNumber == "3"){
if(){
}
else{
}
}
}
else if (BtnInteracted == "Con"){
if(sample_1.SaveSlotNumber == "1"){
if(){
}
else{
}
}
else if(sample_1.SaveSlotNumber == "2"){
if(){
}
else{
}
}
else if(sample_1.SaveSlotNumber == "3"){
if(){
}
else{
}
}
}
to this.
switch(BtnInteracted){
case "New": switch (sample_1.SaveSlotNumber){
case "1": //if() else() statement;
case "2": //if() else() statement;
case "3": //if() else() statement;
} break;
case "Con": switch (sample_1.SaveSlotNumber){
case "1": //if() else() statement;
case "2": //if() else() statement;
case "3": //if() else() statement;
} break;
}
is it even possible to make this code or my original is my only option?
thanks for any reply!
You have to be careful with the break statements, but it will work. You can nest switches, but personally I dont't think inner switches are more readable; I prefer pass stuff to functions:
switch(BtnInteracted)
{
case "New":
{
startFrom(sample_1.SaveSlotNumber);
break;
}
case "Con":
{
continueFrom(sample_1.SaveSlotNumber);
break;
}
}
And then use a new switch in the button
function startFrom(index:int):void
{
trace("start from ", index);
switch(index)
{
case 1:
{
// do something
break;
}
case 2:
{
// do something
break;
}
}
}
function continueFrom(index:int):void
{
trace("continue from", index);
switch(index)
{
case 1:
{
// do something
break;
}
case 2:
{
// do something
break;
}
}
}
In lots of cases you'll need the function multiple times with other input so you dont have you repeat yourself and it keeps clear what happens with a specific case.