Copy link to clipboard
Copied
I get these two errors, can anyone help me out?
Error 1:
Error 2:
thank you, _sp.
op:
correct use is:
switch(o.command){
case somevalue:
//some code
break;
case someothervalue:
//some other code
break;
//etc
}
Copy link to clipboard
Copied
i don't know if the switch command is different in flex than flash, but that's incorrect syntax for using switch in flash. look in the api to see the correct way to use switch in flex.
Copy link to clipboard
Copied
switch statements in flex and flash are exactly the same
Copy link to clipboard
Copied
thank you, _sp.
op:
correct use is:
switch(o.command){
case somevalue:
//some code
break;
case someothervalue:
//some other code
break;
//etc
}
Copy link to clipboard
Copied
Thanks,that helped me with the first error, but I cant get the second error away, any suggestions?
Copy link to clipboard
Copied
-
Copy link to clipboard
Copied
Tried that, doesnt work... anything else?
Copy link to clipboard
Copied
Man I hate when these forums won't let you edit. I read the post wrong.
try needs a catch or finally. It needs to know what to "do" when it fails.
e.g.:
try
{
// this
}
catch (e)
{
// error occured, do something if you want
}
Copy link to clipboard
Copied
Thanks Sinious, got it to work!
One more question, how do I make the "Var NewValue" global, so I can read and use it in an other function..etc?
Copy link to clipboard
Copied
kglad initially helped you so you should mark him as correct answer and you can just mark mine as helpful.
You should declare them at the top of your class and they all need an access modifier. Such as private, protected, internal, public.
e.g.
package {
import flash.display.Sprite;
public class MyClass extends Sprite {
// define your variables here
private var NewValue:String; // create a variable only this class can use by marking it private
// constructor
public function MyClass() {
NewValue = "Some String"; // set the initial value
}
private function SomeFunction():void
{
// this function is in this class so I can modify NewValue
NewValue = "Some Other Value"; // work fine
}
}
}
Copy link to clipboard
Copied
you can't assign an int cast variable to be an Object cast object.
you should be using something like:
var o:Object = event.data.readObject();
switch(o.command){
case one_of_the_values_that_o.command_can_be:
// code for this case
break;
case another_value_that_o.command_can_be:
// code for this other case
break;
.
.
}
if you don't understand how to apply that pseudo code to your situation, paste the possible values of o.command
Copy link to clipboard
Copied
Thanks, I changet it to private var NewValue:Object; and that worked
Copy link to clipboard
Copied
Glad you got it working
Copy link to clipboard
Copied
Thanks guys, I learn a lot like this
I will bug you guys again when I get more challenges!!
Copy link to clipboard
Copied
Hi guys,
I got the code running on my phone but i got one issue. It might be that the phone doesnt listen to the port as it should, but im also wondering
if the code is wrong since the code is stuck at "catch(e:Error)".
The "NewValue = o.command;" seems not to work, is this a wrong way to write it?
The function is listening on port 1000, and catch message with "event.data.readObject();" and get stored in "o:Object"
The "NewValue" is a public Var NewValue:Object;"
So any suggestions?
Code:
Copy link to clipboard
Copied
that continues to make no sense.
there's no reason you should be using a switch statement in that code. use:
protected function udpDataHandler(event.DatagramSocketDataEvent):void{
var o:Object = event.data.readObject();
if(o.command){
NewValue=o.command;
} else {
NewValue=0;
}
}
// and i have no idea what you're doing in timeHandler but myArray must be a very poor choice of names because that can't be an array.
Copy link to clipboard
Copied
Indeed your switch will only ever do 1 thing so there's no reason to put the switch in there. Unless you plan on eventually doing "different things" if o.command differs, ditch the switch as kglad said.
You're also casting e:Error to the base Error class but there may be a fragment of a chance that whatever is happening may not conform to that. I went with the safest approach and left e untyped (*, or dynamic) so it would catch anything at all if it came in. Try adjusting it back to "catch (e)" instead of "catch (e:Error)". ANEs introduce a new layer of complexity with not everything is predictably going to be an error. For full safety you can even provide a default value, "catch (e = null)" but this is what flash does at the moment anyhow. Down the line if type checking gets stricter you'll get an argument error otherwise.
In timerHandler() you probably should check the value of NewValue. If it equals zero then there's no real reason to push it onto what I assume is a stack you're creating from received UDP packets.
Copy link to clipboard
Copied
The graph still wont respond to anything coming in on the port with the code from kglad. With the original code, the graph went in the "catch" right away I started sending values to the port. Now it wont even plot the "else" statement..
For me it seems that the "NewValue" is not being updated...
Copy link to clipboard
Copied
try {} catch() {} has the job of attempting to run code you know may fail. It's a way of gracefully handling problems without your app exiting.
You need to see what is coming in the catch (e). trace(e) and see what you get. There has to be some information in there as to what is going wrong.
Copy link to clipboard
Copied
This might help better, here is most of the code:
The UDP anf Graph works independently when tested as they are attended, but when I put them together, they dont "work" together.
Copy link to clipboard
Copied
From looking at your code most of it looks fine, I have only basic questions. Further, I am as mentioned not familiar with the UDP ANE at all.
One tiny thing is why not run initTimer() from your applicationComplete event? At least you guarantee that udpSocket is created already. So the last line of applicationCompleteHandler() can be initTimer();. Just a small thing to make sure the timer doesn't hit before a socket is made.
Second, as far as I know DatagramSocketDataEvent is part of flash.events (beta in that link at that). Is that really what the UDP ANE is sending back for an event? It could be perfectly fine, I'm just wondering why it doesn't have its own event. Or did they name their version of the event the same thing?
Also have you trace()d what happens on the if (o.command) { trace(o.command); } ? Just to see if anything ever comes in.
Also, in timerHandler(), is it really a good idea to add to myArray if NewValue == 0? Is that 0 useful to you? I presume you're making myArray bindable because whatever graph your using can use the values in it to plot something or such but is 0 useful? To me if it's ever set to 0 then nothing came in over the socket and you shouldn't really do anything, but I don't ultimately understand what you're doing with the socket anyhow.
0 may very well come over the socket so maybe you want to set it to something to flag yourself that nothing came over the socket, like -1. If you ever sense NewValue == -1 then you know to ignore that timer.
I don't see the graph portion so I presume as mentioned that it's just reading the binded data.
Copy link to clipboard
Copied
For me it seems that the DatagramSocketDataEvent is named the same way, and the code work when I run it by itself.
Yes, I use 0 to have a default so I can see if something comes in on the port.
I tried to move the initTimer() in my applicationCompleteHandler() but it doesnt change anything.
Copy link to clipboard
Copied
Elaborate more on what you mean by "they don't work together". What exactly isn't working? Not receiving any UDP packets? Graph not reading the binded array?
Copy link to clipboard
Copied
I cant trace the code since it uses ANE. That is not suported. I wish there was a way to check if im receiving UDP packages.
Its either not receiving UDP or the graph is not updating the new value.
Im stuck...
Copy link to clipboard
Copied
Well I got one way to see if im receiving UDP, I will add a text area and read the incomming UDP....
I will try now..
Ok, I have no tried it, and there is no UDP packages coming in. That wierd, it works alone without the graph code..
OMG...I think I got it....Stupid me . I will post it after I tried it...