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
}
}
}
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
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:
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.
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.
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...
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.
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.
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.
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...
You made NewValue an object. That means you need to assign a property to it. If you want to read a string, simply convert NewValue to a String instead of an object.
Do one of these two things:
1) Add a property to NewValue if you keep it an object, e.g.
NewValue.value = o.command;
2) Change NewValue to a String
// at the top of the class
public var NewValue:String;
// assignment, just how you have it now
NewValue = o.command;
Besides that, you really need to stop adding the switch in there. That switch is absolutely pointless. There is no difference between that switch and this:
if (true) {
messageArea.appendText(o.command);
NewValue = o.command;
}
The code above is pointless. It will happen 100% of the time. So ditch the switch. Just make it:
try
{
var o:Object = event.data.readUTFBytes(event.data.bytesAvailable);
if (o.command)
{
messageArea.appendText(o.command);
NewValue = o.command;
}
else
{
NewValue = ""; // set to empty string (if you change NewValues type to String)
}
}
catch (e)
{
event.data.position = 0;
messageArea.appendText(event.data.readUTFBytes(event.data.bytesAvaila ble));
}
You can use a switch, but if you only provide a default it will do that every single time. That's why it's 100% useless. You need to provide cases for the switch statement for it to do different things based on what comes in.
e.g.
switch(o.command)
{
case "": // contains nothing
event.data.position = 0;
messageArea.appendText(event.data.readUTFBytes(event.data.bytesAvaila ble)); // just write out whatever the bytes were (this is kinda useless too)
break;
default: // it contains SOMETHING, so use it, but containing nothing was handled above
messageArea.appendText(o.command);
NewValue = o.command; // assuming this is type String
break;
}
North America
Europe, Middle East and Africa
Asia Pacific