Results 1 to 7 of 7
Thread: Getter function now working
-
September 5th, 2011, 03:20 PM #185Registered User
postsGetter function now working
I'm confused. I'm trying to use a getter function and have set it up the way I think is should work but it's not working. would someone tell me what I'm doing wrong?
Thanks!
I get:
1170: Function does not return a value.
Code:package { import flash.display.MovieClip; public class Main extends MovieClip { public function Main() { var drager = new ScrollByDrag; addChild(drager); var theIndex = drager.getCurrentIndex(); trace(theIndex + " = CurrentIndex"); } } }Code:package { import flash.display.Sprite; public class ScrollByDrag extends Sprite { public var currentIndex:Number = 5; public function ScrollByDrag() { trace("test"); } public function getCurrentIndex():Number { return currentIndex; } public function setCurrentIndex(theCurrentIndex):Number { currentIndex = theCurrentIndex; } } }
-
September 5th, 2011, 03:27 PM #2
Your declation (public function setCurrentIndex(theCurrentIndex):Number) indicates that that function returns a value. Either change Number to void or make the function actually return a number.
-
September 5th, 2011, 05:13 PM #385Registered User
poststhank you for your response. that definitely makes sense. I used your advice and that allowed me retrieve the currentIndex value through the getCurrentIndex() function. but i'm still having trouble implementing the setter into my code. maybe you can help me to understand this. right now i'm accessing currentIndex and position[] directly through drager:ScrollByDrag. I would like to access them through a getter and setter function as seen previously. currently the function looks like this:
i guess i have to use both getCurrentIndex() and setCurrentIndex(). I think the getter is working but I don't know how to handle the setter with then incrementer ++.Code:function onRight(e:MouseEvent){ if(drager.currentIndex < drager.position.length -1){ drager.currentIndex ++; } else{ drager.currentIndex = drager.position.length -1; } TweenMax.to(drager.content, 1, {x: drager.position[drager.currentIndex],ease:Quad.easeOut}); //trace(drager.currentIndex); }
can this work? thanks!Code:function onRight(e:MouseEvent){ if(drager.getCurrentIndex() < drager.position.length -1){ drager.setCurrentIndex() ++; } else{ drager.setCurrentIndex() = drager.position.length -1; } TweenMax.to(drager.content,drager.position[drager.getCurrentIndex()],ease:Quad.easeOut}); }
-
September 5th, 2011, 05:52 PM #4
While your functions are getters and setters by definition, they're not what AS3 would call getters and setters in the strictest sense of the term. AS3 has implemented accessor methods using the specific keywords get and set. If you're interested in trying this approach, this might be a good read for you: http://www.kirupa.com/forum/showthre...=1#post2572477
And if my explanation there doesn't work for you, there's like 20 more in that thread that may be better.
If you want to keep going with your method, then the code should look something like this:
Code:function onRight(e:MouseEvent){ if(drager.getCurrentIndex() < drager.position.length -1){ drager.setCurrentIndex(drager.getCurrentIndex()++); } else{ drager.setCurrentIndex(drager.position.length -1); } TweenMax.to(drager.content,drager.position[drager.getCurrentIndex()],ease:Quad.easeOut}); }Proud Montanadian
We tolerate living and breathing. And niches.
Name Brand Watches
Maybe getTimer() or TweenMax is the answer to your problem . . .
-
September 5th, 2011, 06:42 PM #585Registered User
postsThank you very much for your response and for the very useful link. There is still one thing that I don't understand. in order to increment currentIndex I had to store it in a var theIndex I don't know why but at least it works now.
Also I have a general question about this method v.s. the get and set key words. Is the method I have used generally frowned upon? From my perspective, It seems a bit more transparent and understandable to leave it as an obvious function call. Colin Moock in the essential actionscript 3.0 (I know this book is a bit old) seems to prefer the method I have used. would you mind sharing your thoughts on this.Code:public class Main extends MovieClip { private var theIndex; public function Main() { function onRight(e:MouseEvent){ if(drager.getCurrentIndex() < drager.position.length -1){ drager.setCurrentIndex(theIndex ++); } else{ drager.setCurrentIndex(drager.position.length -1); } TweenMax.to(drager.content, 1, {x: drager.position[drager.getCurrentIndex()],ease:Quad.easeOut}); } } }
thanks a lot!!!
-
September 5th, 2011, 08:47 PM #6
Well for one, as you discovered, you can't increment the data when the accessors are written as you have. I don't really know what you're doing but it seems kind of silly to have to keep track of the index both in and outside of the class where it is being used. Using the get and set keywords, you can increment the proxy property that is created (if that makes any sense). Basically you can do this:
As for the general consensus on what people prefer, I have no idea. It's mostly preference I think. I personally don't like typing getProp and setProp when I can just type prop. I think properties are meant to store data and methods are meant to do things with this data, so using a method to access stored data is counter-intuitive for me. Kind of like how you don't use a verb in place of a noun and vice-versa.Code:package { public class Test extends Object { private var _data:uint = 0; public function get data():uint { return _data; } public function set data(v:uint):void { _data = v; } } } var t:Test = new Test(); t.data++; trace(t.data); //1
This thread slightly brushes on the topic but it's not very related:
http://www.kirupa.com/forum/showthre...her-use-.-.-.&
On another note, I had no idea the code I posted earlier wouldn't work and I find it really stupid that it doesn't . . .Proud Montanadian
We tolerate living and breathing. And niches.
Name Brand Watches
Maybe getTimer() or TweenMax is the answer to your problem . . .
-
September 5th, 2011, 10:25 PM #785Registered User
poststhat is a good reason.
Thanks again! I very much appreciate your help and insight.

Reply With Quote

Bookmarks