PDA

Volledige versie bekijken : Class probleem II


Fatty Owl
%Europe/Berlin %819 %2005, 19:39
Sorry, weer een class probleem. Mijn variables worden undefined in de volgende code (Niet al mijn code staat er)
De trace traced undefinedclass Tool {
var Tcount:Number = 0;
if (currentTool == "Text") {
line.onMouseDown = function() {
var Textfield:TextField = _root.createTextField("inputTekst"+Tcount, Tcount+10000, _xmouse, _ymouse, 500, 100);
trace(_root.Tcount);
Tcount++;
Textfield.type = "input";
Textfield.border = true;
};
}
}
}

Voetsjoeba
%Europe/Berlin %828 %2005, 19:53
Vanwaar komt die _root opeens ? Probeer eens gewoon trace(Tcount);

Fatty Owl
%Europe/Berlin %832 %2005, 19:58
NaN....

Voetsjoeba
%Europe/Berlin %870 %2005, 20:54
Staat die var declaratie in een functie of als classvar ?

TheDutch
%Europe/Berlin %891 %2005, 21:24
Ik dacht dat je zei dat je het boek Essential ActionScript 2.0 had gekocht? Zeker nog niet echt veel van gelezen :P.

Wat wil je precies bereiken? Dit wil ik weten omdat ik anders niet weet hoe ik je kan vertellen hoe het wel zou moeten :).

Roenes
%Europe/Berlin %897 %2005, 21:32
Geef eens de hele code van je class. Want hetgene wat er nu staat zou foutmeldingen moeten geven want je begint ineens met een ifje terwijl die niet in een functie of iets staat. :)

Fatty Owl
%Europe/Berlin %937 %2005, 22:29
Ik dacht dat je zei dat je het boek Essential ActionScript 2.0 had gekocht? Zeker nog niet echt veel van gelezen :P.


k zit al aan p 15 :p Ik wil bereiken dat telkens als er een nieuw textfield gemaakt wordt die in een nieuwe depth en met een nieuwe naam komt.
mijn hele class is dit: class Tool {
private var line:MovieClip;
private var lineCount:Number = 100;
private var currentTool:String;
public var bgcolor:String = "0xFFFFFF";
public var Tcount:Number = 0;
public function setTool(brushName:String):Void {
currentTool = brushName;
}
public function getTool() {
return currentTool;
}
public function getBGcolor() {
return bgcolor;
}
public function startDraw(Size:Number, myColor:String, Alpha:Number):Void {
line = _root.createEmptyMovieClip("line"+lineCount, lineCount);
lineCount++;
/*GOM TOOL PROPERTIES*/
line.onEnterFrame = function() {
bgcolor = "0xFFFFFF";
if (this._parent.currentTool == "Gom") {
this.lineStyle(Size, bgcolor, Alpha);
} else {
this.lineStyle(Size, myColor, Alpha);
}
};
/*BRUSH TOOL PROPERTIES*/
if (currentTool == "Brush") {
line.onMouseDown = function() {
this.onMouseMove = function() {
this.lineTo(_xmouse, _ymouse);
updateAfterEvent();
};
this.moveTo(_xmouse, _ymouse);
};
line.onMouseUp = function() {
delete this.onMouseMove;
};
}
if (currentTool == "Text") {
line.onMouseDown = function() {
var Textfield:TextField = _root.createTextField("inputTekst"+Tcount, Tcount+10000, _xmouse, _ymouse, 500, 100);
trace(Tcount);
Tcount += 2;
Textfield.type = "input";
Textfield.border = true;
};
}
}
}

c0redump
%Europe/Berlin %945 %2005, 22:41
die Tcount komt helemaal niet voor in je line object lijkt t zo...das een var in de Tool klasse.
als je dit doet: line.onMouseDown = function(){ ... };
dan geef je eht onMouseDown event van 'line' een functie. Variabelen als 'Tcount' kan je dus ook lezen als 'this.Tcount' en aangezien het een functie binnen het 'line' object is, slaat 'this' op 'line'...

Fatty Owl
%Europe/Berlin %963 %2005, 23:07
ok dat snap ik maar hoe kan ik dan aan tcount ?

TheDutch
%Europe/Berlin %994 %2005, 23:52
Even heel kort gedacht. Wat dacht je van zoiets? Dit stop je in een nieuwe method van jouw class:

private function methodNaam():Void{
trace(Tcount);
}

En dan dit als onEnterFrame:

line.onEnterFrame = Delegate.create(this, methodNaam);

Dauntless
%Europe/Berlin %999 %2005, 23:59
En bovenaan zet je:
import mx.utils.Delegate;

TheDutch
%Europe/Berlin %018 %2005, 00:26
En mocht het zo zijn dat je nu denkt dat je geen argumenten kan meegeven aan die functie, dan heb je daar helemaal gelijk in! Gelukkig heeft een oud-collega van mij EECOLOR de Delegate class herschreven zodat je wel argument kunt meegeven aan de method create() :).

class Delegate extends Object {
static function create(obj:Object, func:Function):Function {
var returnFunction = function () {
var self:Function = arguments.callee;
var target_obj:Object = self.target_obj;
var func:Function = self.func;
var userArguments_array:Array = self.userArguments_array;
return func.apply(target_obj, arguments.concat(userArguments_array));
};
returnFunction.target_obj = obj;
returnFunction.func = func;
returnFunction.userArguments_array = arguments.splice(2);
return returnFunction;
}
}

Dauntless
%Europe/Berlin %028 %2005, 00:40
Narie heeft die ook herschreven dacht ik :). Moet ergens in het ASSC staan...

TheDutch
%Europe/Berlin %032 %2005, 00:46
Klopt hier (http://www.flashfocus.nl/forum/showthread.php?t=2590) :).

Fatty Owl
%Europe/Berlin %459 %2005, 11:01
ok, ik heb de delegate class die theDutch heeft gepost in een een as file met de naam delegate gezet. Moet ik die import er dan ook nog bijzetten? ik heb ook die onEnterFrame veranderd in een onMouseDown :). en dan heb ik dit: class Tool {
private var line:MovieClip;
private var lineCount:Number = 100;
private var currentTool:String;
public var bgcolor:String = "0xFFFFFF";
private var Tcount:Number = 0;
private function getVariable():Void {
trace(Tcount);
}
public function setTool(brushName:String):Void {
currentTool = brushName;
}
public function getTool() {
return currentTool;
}
public function getBGcolor() {
return bgcolor;
}
public function startDraw(Size:Number, myColor:String, Alpha:Number):Void {
line = _root.createEmptyMovieClip("line"+lineCount, lineCount);
lineCount++;
/*GOM TOOL PROPERTIES*/
line.onEnterFrame = function() {
bgcolor = "0xFFFFFF";
if (this._parent.currentTool == "Gom") {
this.lineStyle(Size, bgcolor, Alpha);
} else {
this.lineStyle(Size, myColor, Alpha);
}
};
/*BRUSH TOOL PROPERTIES*/
if (currentTool == "Brush") {
line.onMouseDown = function() {
this.onMouseMove = function() {
this.lineTo(_xmouse, _ymouse);
updateAfterEvent();
};
this.moveTo(_xmouse, _ymouse);
};
line.onMouseUp = function() {
delete this.onMouseMove;
};
}
if (currentTool == "Text") {
line.onMouseDown = Delegate.create(this, getVariable);
line.onMouseDown = function() {
var Textfield:TextField = _root.createTextField("inputTekst"+Tcount, Tcount+10000, _xmouse, _ymouse, 500, 100);
Tcount += 2;
Textfield.type = "input";
Textfield.border = true;
};
}
}
}

maar dit werkt nog niet :).

TheDutch
%Europe/Berlin %495 %2005, 11:52
Ik zie ik twee keer line.onMouseDown, dat is denk ik een foutje. Wanneer je Delegate.as in dezelfde map hebt als de SWF dan hoef je hem niet te importeren.

Ik zou als ik jou was wel een verschil kenbaarmaken tussen class instance variables en method argumenten en tussen public methods en private methods. Mijn advies is om class instance variable een prefix _ te geven en private class methods __ als prefix. Op die manier is het allemaal een stuk duidelijker en minder foutgevoelig.

Het ziet er verder goed uit :).