Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

A constructor cannot specify a return type.

New Here ,
Feb 11, 2009 Feb 11, 2009
I have a shuffleList.as file with this code...
package {
import flash.display.MovieClip;
public class shuffleList extends MovieClip{
function shuffleList(arr:Array):Array{
var shuffled:Array = arr.slice();
for (var i:int=0; i<arr.length; i++) {
var element:Object = shuffled ;
var rnd:int = Math.floor(arr.length * Math.random());
shuffled
= shuffled[rnd];
shuffled[rnd] = element;
}
return shuffled;
}
}
}
In the first frame i put this code...

import shuffleList

var numA:Array = [0,1,2,3,4,5,6,7,8,9,10]
var numB:Array = shuffleList(numB);
var myText:String = numB.pop()
fieldA.text = myText
With this code I want to put in random order the numbers from 0 to 10 in the fieldA.

When run this file the compiler report this error: "A constructor cannot specify a return type."
Where I'm wrong;
I'm new in actionscript..
Please help...

TOPICS
ActionScript
1.5K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 11, 2009 Feb 11, 2009
I'm no Class expert, but my first eyeballing of it says to declare that function as public... the class is public, but I don't know what status the function has without being declared as private or public.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Feb 11, 2009 Feb 11, 2009
Hey Petros,
I had a similar problem and the response I got was:

1) the constructor is used to set up the class but it doesn't really exist until it is instantiated and therefore cannot include any 'interaction' including a return.

2) Therefore, you might try replacing

function.shuffleList ... with

function.shuffleList() { // to enable instantiation
}
public function doit(arr:Array):Array { // to enable interaction

The rest of the code follows this then the call in the first frame replaces

var numB:Array ... with

new shuffleList(); // instantiate the class
var numB:Array = shuffleList.doit(numB) // interact with the class

Let me know how it goes...Daniel
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 11, 2009 Feb 11, 2009
Daniel,
Thank's for your answer..
I change the code as you said and now the compiler report this error:
1067: Implicit coercion of a value of type shuffleList to an unrelated type Array.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 11, 2009 Feb 11, 2009
Daniel,

I didn't replaced the code in the frame...
So the new code as you said is...

18.var numA:Array = [0,1,2,3,4,5,6,7,8,9,10]
19.new shuffleList(); // instantiate the class
20.var numB:Array = shuffleList.doit(numA) // interact with the class

and now the compiler report this error:

Line 19:1180: Call to a possibly undefined method shuffleList.
Line 20: 1120: Access of undefined property shuffleList.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 11, 2009 Feb 11, 2009
LATEST
If i put the function in the frame
everything is O.K.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines