|
-
no default constructor found in base class ball
i want the class BeginBall to extend Ball, but when i do so, this error shows up.
this is my code:
PHP Code:
package
{
import flash.events.Event;
import flash.display.MovieClip;
public class Ball extends MovieClip
{
public var nSpeedX:Number;
public var nSpeedY:Number;
public var ballNr:int;
const BALLDIAMETER = 10;
private var nStageWidth:Number = 500;
private var nStageHeight:Number = 400;
public function Ball ( X,Y,NSpeedX:Number, NSpeedY:Number ) : void
{
nSpeedX = NSpeedX;
nSpeedY = NSpeedY;
x = X;
y = Y;
this.addEventListener ( Event.ENTER_FRAME, onEnterFrameHandler );
}
private function onEnterFrameHandler ( E:Event ) : void
{
this.x -= nSpeedX;
this.y -= nSpeedY;
if ( this.x >= nStageWidth - BALLDIAMETER - 16 )
{
this.x = nStageWidth - BALLDIAMETER - 16;
nSpeedX *= -1;
}
else if ( this.x <= BALLDIAMETER + 16 )
{
this.x = BALLDIAMETER + 16;
nSpeedX *= -1;
}
if ( this.y >= nStageHeight - BALLDIAMETER - 16 )
{
this.y = nStageHeight - BALLDIAMETER - 16;
nSpeedY *= -1;
}
else if ( this.y <= BALLDIAMETER + 16 )
{
this.y = BALLDIAMETER + 16;
nSpeedY *= -1;
}
}
}//end of class
public class BeginBall extends Ball
{
//alles over spatieknop en muisknop etc.
}
}
i tried to do public last class Ball, butit says access identifiers are not allowed with namespace attributes.
how can i let BeginBall normally inherit Ball's constructor, or if its not possible, make a new one for BeginBall?
-
OOP is one letter from OOPS
Try moving the helper class BeginBall outside of the package brackets.
-
doesn't work. same error.
-
OOP is one letter from OOPS
How are you trying to instantiate it?
-
PHP Code:
var shootBall:ShootBall = new ShootBall; addChild(shootBall);
changed the name in ShootBall btw, it's more suitable.
-
OOP is one letter from OOPS
also try:
Constants
ActionScript 3.0 supports the const statement, which you can use to create constants. Constants are properties with a fixed value that cannot be altered. You can assign a value to a constant only once, and the assignment must occur in close proximity to the declaration of the constant. For example, if a constant is declared as a member of a class, you can assign a value to that constant only as part of the declaration or inside the class constructor.
The following code declares two constants. The first constant, MINIMUM, has a value assigned as part of the declaration statement. The second constant, MAXIMUM, has a value assigned in the constructor.
class A
{
public const MINIMUM:int = 0;
public const MAXIMUM:int;
public function A()
{
MAXIMUM = 10;
}
}
-
OOP is one letter from OOPS
Well you define parameters for the constructor, but you are not passing any. a "default" constructor is one that takes no parameters, so what it is telling you is that it can not find the constructor defined in the way you are trying to use it.
-
ooh stupid me.
ps. i use constants. but u probably mean the 16 ? yeah i should change that into a constant.
edit: i now assigned the right variables in the instanciation, but the same problem exists, only this time it gives the same error twice. :/
Last edited by omniscient232; 08-19-2008 at 10:12 AM.
-
OOP is one letter from OOPS
I was actually referring to the fact that you did not explicitly declare an access modifier before it. Which I guess works and would default to internal I believe.
-
huh?? so what do i have to do?
-
half as fun, double the price
Glancing at the code it looks like you have two classes in the same package. You should break them out into separate class files.
When you extend Ball, the constructor of the subclass must call super with the necessary arguments ( X,Y,NSpeedX:Number, NSpeedY:Number ) so that the Ball constructor for the subclass instance can be called correctly. Similarly, make sure you create your subclass instance with the right number of arguments... which is what I assume you fixed in your last post?
Last edited by senocular; 08-19-2008 at 10:44 AM.
-
OOP is one letter from OOPS
The problem is with your sub class. Since it extends Ball, it would have to construct ball when it is created and since it does not provide any parameters for the ball constructor, you get your error message.
-
OOP is one letter from OOPS
LOL he beat me to it. Thanks Senocular.
-
i put it in a different as file in the same package, but it doesnt work. do i have to put it in another package? that would kinda annoy me. can't i do it someway different?
-
half as fun, double the price
They don't have to be in different pacakges. Packages are just for organization.
You just need to make sure you call super() where needed and with the right arguments.
You may need to repost what code you're using now.
-
only the shootball instantiation changed:
var shootBall:ShootBall = new ShootBall(SHOOTBALLDOWN_X,SHOOTBALL_Y,0,0);
what is super()?
-
half as fun, double the price
You haven't posted the ShootBall class.
super() is what lets you access superclass members, including the constructor. When you create a subclass of another class that is defined with required parameters in the constructor, your subclass is responsible for making sure those parameters are taken care of by passing arguments into those parameters of the superclass constructor. You call the superclass constructor through super.
So in your ShootBall constructor, you must call super using whatever values you see fit to define for ( X,Y,NSpeedX:Number, NSpeedY:Number )
Code:
public function ShootBall(){
super(0,0, 10, 5);
}
-
this is what i have now, but it still doesnt work.(ignore everything under the constructor)
PHP Code:
package { public class ShootBall extends Ball { private var touchable:Boolean = false; private var speedingLvl; alles over spatieknop en muisknop etc. public function ShootBall( X,Y,NSpeedX:Number, NSpeedY:Number) { super(X,Y,NSpeedX:Number, NSpeedY:Number) } function muisKnopKlik(event:Event) { //balkje begint te gaan speedingLvl = 0; } function muisKnopLos(event:Event) { mouseX - x mouseY - y function shootShootBall(hspeed,vspeed) { nSpeedX = hspeed; nSpeedY = vspeed; touchable = true; } } } }
-
half as fun, double the price
asside from the misplaced "alles over spatieknop en muisknop etc." it seems passable.
Now, if you create an instance of ShootBall, you will need to be sure to pass in X,Y,NSpeedX:Number, NSpeedY:Number to that (not present in your previous code).
-
OOP is one letter from OOPS
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|