Sunday, April 4, 2010

Passing argument from a when using one...

I had a class that I was passing a simple argument to like so:

var quiz_1_2:CaseStudyQuiz = new CaseStudyQuiz(2);

addChild(quiz_1_2);

I now would like to use this class for multiple MCs in my library. I thought I could let Flash create a class for each of them and specifying the CaseStudyQuiz class as the Base class. When I do that, I get the following error:

1136: Incorrect number of arguments.?Expected 0.

Passing argument from a when using one...

Do you have the base class extending MovieClip?

Also, once you do that you will make instances of the sub class... so if your clips class name is penny, and the base class is CaseStudyQuiz you could do:

var a = new penny();

If you then wanted to init some function in your base class you could have an init method in it:

a.init(2);

hth

Passing argument from a when using one...

Please show the code for CaseStudyQuiz class constructor.

What does function CaseStudyQuiz expect?

Sorry, I wasn't in reach of a computer over the weekend.

My constructor function looks like this:

public function CaseStudyQuiz(corretOne:uint):void

{

correctAnswer = corretOne;

initBtns();

initAnswer();

quizTimer = new Timer(1000, 6000);

quizTimer.addEventListener(TimerEvent.TIMER, timerCheck);

quizTimer.start();

addEventListener(Event.ADDED_TO_STAGE, mousemoveF);

}

Constructor looks good. Can you post how you wrote functiond timerCheck and mousemoveF - how you pass arguments into them.

I attached the whole class. Thank you so much for helping me here!

The issue is that this is the base class and I would like to now have to write a custom class for each MC I want to use this with.

I just tested your class the way you instantiate it works perfectly. Of course I commented out all the references to the objects that are on stage or library.

It seams that the error you describe comes from elsewhere.

Well it wasn't passing the parameter from the MC class to the base class. Maybe I was not very good at explaining it. This post does a better job at what I was trying to explain.

http://stackoverflow.com/questions/680019/passing-parameters-to-constructor-for- an-as3-auto-created-asset-class

Here is what I did instead of passing the parameter directly and this seems to work.

I took all the code from the constructor function of the base class, i.e. CaseStudyClass and put it into a new method called: initQuiz.

Then when I instantiate the movie clip, I did it like this:

var quiz_1_2:Quiz_2_1 = new Quiz_2_1();

addChild(quiz_1_2);

quiz_1_2.initQuiz(2);

And that solves the problem for me!

Thank you so much for the help!!!

I am glad you found a solution. But...

Do your changes mean that for each instance you have a separate class? If so, it sort of, defies the purpose of OOP one of conveniences of which is recycling objects.

Why could not you add the method initQuiz() to your original Quiz class and then reuse it:

var quiz_1_2:Quiz = new Quiz();

addChild(quiz_1_2);

quiz_1_2.initQuiz(2);

var quiz_1_3:Quiz = new Quiz();

addChild(quiz_1_3);

quiz_1_3.initQuiz(3);

etc...

I still believe that your original approach is much better and the error was not related to the Quiz class.

No, I only have one class. That was really important to me, as yes I agree, that would completely defeat the purpose.

I am doing this:

var quiz_1_2:Quiz = new Quiz();

addChild(quiz_1_2);

quiz_1_2.initQuiz(2);

var quiz_1_3:Quiz = new Quiz();

addChild(quiz_1_3);

quiz_1_3.initQuiz(3);

etc...

So all is good in OOP world!

Thank you so much for the help!!!

Wait!

I am doing this:

var quiz_1_2:Quiz_2_1 = new Quiz_2_1();

addChild(quiz_1_2);

quiz_1_2.initQuiz(2);

var quiz_1_3:Quiz_1_3 = new Quiz_1_3();

addChild(quiz_1_3);

quiz_1_3.initQuiz(2);

etc...

Yeah, and this is what I was talking about. The approach in your previous post when you instantiated Quiz make more sense to me.

In addition to what I said before, it looks like you differentiate the objects by two integers, so nothing prevents you from passing two variables into initQuiz() method (just add an argument in the class):

var quiz_1_2:Quiz_2_1 = new Quiz();

addChild(quiz_1_2);

quiz_1_2.initQuiz(1, 2);

var quiz_1_3:Quiz_1_3 = new Quiz();

addChild(quiz_1_3);

quiz_1_3.initQuiz(1, 3);

No comments:

Post a Comment