Skip to main content
Participant
June 20, 2016
Question

Question about game coding [Actionscript]

  • June 20, 2016
  • 1 reply
  • 543 views

Hello Actionscript 3 Wizards,

I am looking to make a game in Adobe Animate CC. I have most of the Art assets drawn. I am looking for help with the code to do the following:

An object needs to be called at random from the library. The object ideally has a “Key” that corresponds to either Right or Wrong, True or False. Essentially the picture is a question that can be right or wrong based on if they choose Button A or Button B.

For Example, if they choose Button A, and it’s correct, I would like the score to update 2 points, and for the script to call the next object and start the process again.

Vice Versa, if it’s incorrect, the score is deducted and the process again repeats with a new object and possibility of it being right or wrong.

I apologize for my English, and thank you for any help I can get.

Please see the attached for a visual.

http://prntscr.com/bivdn4

This topic has been closed for replies.

1 reply

kglad
Community Expert
Community Expert
June 21, 2016

your library objects each need a class name.  if those are chosen wisely, you can save some coding time.

eg, if you have Q1,Q2,.. classes, you can use the following to randomize question presentation and get started on your coding:

var C:Class;

var i:int;

var n:int = number of questions

var qIndex:int = 0;  // question index

var q_mc:MovieClip;  // assuming your questions are movieclips

var qA:Array = [];

for(i=0;i<n;i++){

qA.push(i);

}

shuffle(qA);

function nextQuestionF():void{

C=Class(getDefinitionByName('Q'+qA[qIndex]);

q_mc=new C();

q_mc.index = qA[qIndex];  // use this to retrieve info about the question

// probably you'll want to add to display here

}

function shuffle(a:Array) {

    var p:int;

    var t:*;

    var ivar:int;

    for (ivar = a.length-1; ivar>=0; ivar--) {

        p=Math.floor((ivar+1)*Math.random());

        t = a[ivar];

        a[ivar] = a

;

        a

= t;

    }

}

Participant
June 21, 2016

That's amazing and super helpful. Thank you!

Why do the questions have to be classes? Why can't I just use the instance names to call upon them?

kglad
Community Expert
Community Expert
June 21, 2016

if they're in your library like you mentioned, you have to use classes to create instances.

if they're already on-stage, they're already instances and classes aren't needed.