PDA

Volledige versie bekijken : Class probleem


Fatty Owl
%Europe/Berlin %591 %2005, 14:11
ik ben voor te oefenen een class aan het schrijven voor een rechthoek te tekenen. Ik heb begrepen uit de errors dat een class geen createEmptyMovieClip kan gebruiken, ook niet binnen een functie? hoe kan ik dit dan oplossen? dit is mijn code:
class Box {
public var Width:Number;
public var Height:Number;
public function setBoxSize(W:Number, H:Number) {
if (W == undefined) {
W = 50;
}
if (H == undefined) {
H = 50;
}
Width = W;
Height = H;
}
public function setLineStyle(Linethickness:Number, Linecolor:String, Fillcolor:String, Fillalpha:Number) {
var LineStyle:Array = [Linethickness, Linecolor, Fillcolor, Fillalpha];
}
public function Draw() {
var box:MovieClip = _root.createEmptyMovieClip("box", 10);
box.lineStyle(LineStyle[0], LineStyle[1], 100);
if (LineStyle[2] !== undefined) {
if (LineStyle[3] == undefined) {
LineStyle[3] = 100;
}
box.beginFill(LineStyle[2], LineStyle[3]);
}
box.lineTo(0, Width);
box.lineTo(Width, Height);
box.lineTo(Width, 0);
box.lineTo(0, 0);
}
}

En dit zijn de errors die ik krijg als ik typ var box:Box = new Box(); :
**Error** C:\Documents and Settings\Kobe\My Documents\AS\Box.as: Line 19: There is no property with the name 'LineStyle'.
box.lineStyle(LineStyle[0], LineStyle[1], 100);

**Error** C:\Documents and Settings\Kobe\My Documents\AS\Box.as: Line 20: There is no property with the name 'LineStyle'.
if (LineStyle[2] !== undefined) {

**Error** C:\Documents and Settings\Kobe\My Documents\AS\Box.as: Line 21: There is no property with the name 'LineStyle'.
if (LineStyle[3] == undefined) {

**Error** C:\Documents and Settings\Kobe\My Documents\AS\Box.as: Line 22: There is no property with the name 'LineStyle'.
LineStyle[3] = 100;

**Error** C:\Documents and Settings\Kobe\My Documents\AS\Box.as: Line 24: There is no property with the name 'LineStyle'.
box.beginFill(LineStyle[2], LineStyle[3]);

Total ActionScript Errors: 5 Reported Errors: 5

Roenes
%Europe/Berlin %599 %2005, 14:23
je createEmptyMovieClip werkt gewoon naar behoren hoor :) De foutmelding geeft aan dat Linestyle niet bestaat in de functie draw. Dat klopt want je hebt Linestyle locaal gecreëerd in een andere methode. Dit zou moeten werken:

class Box {
public var Width:Number;
public var Height:Number;
public var Linestyle:Array;
public function setBoxSize(W:Number, H:Number) {
if (W == undefined) {
W = 50;
}
if (H == undefined) {
H = 50;
}
Width = W;
Height = H;
}
public function setLineStyle(Linethickness:Number, Linecolor:String, Fillcolor:String, Fillalpha:Number) {
LineStyle:Array = [Linethickness, Linecolor, Fillcolor, Fillalpha];
}
public function Draw() {
var box:MovieClip = _root.createEmptyMovieClip("box", 10);
box.lineStyle(LineStyle[0], LineStyle[1], 100);
if (LineStyle[2] !== undefined) {
if (LineStyle[3] == undefined) {
LineStyle[3] = 100;
}
box.beginFill(LineStyle[2], LineStyle[3]);
}
box.lineTo(0, Width);
box.lineTo(Width, Height);
box.lineTo(Width, 0);
box.lineTo(0, 0);
}
}
Probeer trouwens wel op conventies te letten: laat bv alleen classes met een hoofdletter beginnen en alle andere dingen met een kleine letter :)

Fatty Owl
%Europe/Berlin %606 %2005, 14:32
ok, zo werkt hij zonder errors, maar hij doet niets als ik dit in de fla typ: var box:Box = new Box();
box.setBoxSize(100, 100);
box.setLineStyle(1, "0x000000", "0xFEBB56", 100);
box.Draw();

normaal moet hij toch een vierkant tekenen nu?

Roenes
%Europe/Berlin %612 %2005, 14:41
Sorry, er zaten nog 2 kleine foutjes in (typfoutje en :Array te veel). Werkende code is dit:

class Box
{
public var Width:Number;
public var Height:Number;
public var LineStyle:Array;

public function setBoxSize(W:Number, H:Number) {
if (W == undefined) W = 50;
if (H == undefined) H = 50;
Width = W;
Height = H;
}

public function setLineStyle(Linethickness:Number, Linecolor:String, Fillcolor:String, Fillalpha:Number)
{
LineStyle = [Linethickness, Linecolor, Fillcolor, Fillalpha];
}

public function Draw()
{
var box:MovieClip = _root.createEmptyMovieClip("box", 10);
box.lineStyle(LineStyle[0], LineStyle[1], 100);
if (LineStyle[2] !== undefined)
{
if (LineStyle[3] == undefined)
{
LineStyle[3] = 100;
}
box.beginFill(LineStyle[2], LineStyle[3]);
}
box.lineTo(0, Width);
box.lineTo(Width, Height);
box.lineTo(Width, 0);
box.lineTo(0, 0);
}
}

Fatty Owl
%Europe/Berlin %619 %2005, 14:52
thx zo werkt ie :):D