| Ultrashock Forums
• Custom Event with multiple EventTypes and data |
Member Blogs | ||
![]() |
||||
| Search this Thread | Thread Tools | Display Modes |
|
|
|||||||||||||||||||||||||
![]() |
Ultrashock Member Comments:
|
2007-10-29
#2 |
||
|
whatever class you're defining handleLogin_Result in needs to be an event dispatcher, either through extending EventDispatcher (or a subclass, like Sprite) or by implementing IEventDispatcher and using the EventDispatcher class to dispatch events for your instance in the implemented methods
|
|
|
|
2007-10-29
#3 |
||
|
I had tried that but it still didn't seem to work. Originally this was a static class. I changed it all over to not be static and now it all seems to work. I can't see that being the only cause tho. Static classes should be able to dispatch events, shouldn't they? Here's the constructor of the class as it was in the non-working state. Code:
package {
import flash.net.NetConnection;
import flash.net.Responder;
import flash.events.*;
import flash.display.*;
import RemotingEvent;
public class Remoting extends Sprite {
private static const SERVICE_URL:String = "http://www.theurl.com/path/to/gateway.php";
private static var service:NetConnection;
// Constructor
public function Remoting() {}
// Sends the users login information to the server for validation.
public static function login(u:String, p:String):void {
// This method sends the login data to the server
var responder = new Responder(handleLogin_Result, onFault);
service.call("gateway.login", responder, u, p);
}
private static function handleLogin_Result(isValid:Array):void {
// param @ isValid:Array -- Contains 2 items. [0]=="success or error", [1]=="The error string"
var responseObj:Object = {isAllowed:false, errMsg:""};
var errStr:String = isValid[0].toLowerCase();
var evt:RemotingEvent;
// check to see if the user has passed in a proper username and password
if (errStr == "error") {
// Error has occurred.
// Username / Password is incorrect.
evt = new RemotingEvent(RemotingEvent.LOGIN_RESULT, {err:errStr});
} else if (errStr == "success") {
// Allow the user to login.
evt = new RemotingEvent(RemotingEvent.LOGIN_RESULT, {err:errStr});
}
dispatchEvent(evt);
}
}
}
The way I ended up going just takes out all of the static types which forced me to create a Remoting Object instead of just calling it directly. Code:
// Old Remoting.login(u, p); // New var remObj:Remoting = new Remoting(); remoObj.login(u, p); If you see what I've messed up I'd be interested to know what it was/is. Or if I'm completely out to lunch in my thinking and there is a better way I'd like to hear that as well. Thanks Senocular. |
|
|
2007-10-29
#4 |
||
|
static methods can't dispatch because the "instance" is the class itself, and that is an instance of Class which does not inherit from EventDispatcher.
|
|
1 Blog Entries
13 Creative Assets
|
2007-10-29
#5 |
||
|
It should work with a singleton though.
|
|
|
|
2007-10-31
#6 |
||
|
I think I'm going to end up going the singleton route as this particular class is going to be used in so many places within the app. I don't want to have multiple login's of the same user etc. so it's probably the best way to go for now. This was why I was trying to keep the static class. Thanks guys.
|
|
1 Blog Entries
13 Creative Assets
|
2007-10-31
#7 |
||
|
Yeah, a singleton would be better IMO. They pretty much behave the same way as all-static classes do. I think the only time all-static classes are really needed is for utility classes and classes that simply contain parameter values (i.e. StageAlign and StageScaleMode etc). It sounds like you are already familiar with singletons but I thought I would post the following code anyway. This is how I do things: ActionScript Code:
ActionScript Code:
|
|
1 Blog Entries
|
2007-10-31
#8 |
||
|
not really an unwritten rule of OOP that says to use 'getInstance'... it's more because not all languages support implicit getter/setter methods, therefore the 'standard' way for singletons is via getInstance() but you can call it kickMe() if you wanted to, it wouldn't really matter.
|
|
| Thread Tools | |
| Display Modes | Rate This Thread |
|




7 comments
| 187 views


1 Blog Entries
13 Creative Assets
Linear Mode