I am not understanding something I believe that flex can run actionscript but every time I try I get different errors.
this time my error is:
Description Resource Path Location Type
Could not resolve <fx:script> to a component implementation. as.mxml /ar/src line 7 Flex Problem
I keep looking at flex tutorials and it's almost like they assume that one knows the ide hence they are just showing the .as code.
I think that everything flash can do flex can do. So why be restricted to only one.
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
<fx:script>
var socket:XMLSocket;
stage.addEventListener(MouseEvent.CLICK, doConnect);
function doConnect(evt:Event):void{
stage.removeEventListener(MouseEvent.CLICK, doConnect);
socket = new XMLSocket("127.0.0.1", 9001);
socket.addEventListener(Event.CONNECT, onConnect);
socket.addEventListener(IOErrorEvent.IO_ERROR, onError);
}
function onConnect(evt:Event):void{
trace("Connected");
socket.removeEventListener(Event.CONNECT, onConnect);
socket.removeEventListener(IOErrorEvent.IO_ERROR, onError);
socket.addEventListener(DataEvent.DATA, onDataReceived);
socket.addEventListener(Event.CLOSE, onSocketClose);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUp);
}
function onSocketClose(evt:Event):void{
trace("Connection Closed");
stage.removeEventListener(KeyboardEvent.KEY_UP, keyUp);
socket.removeEventListener(Event.CLOSE, onSocketClose);
socket.removeEventListener(DataEvent.DATA, onDataReceived);
}
function keyUp(evt:KeyboardEvent):void{
if(evt.keyCode == 81){
socket.send("exit");
}
else{
socket.send(evt.keyCode);
}}
function onDataReceived(evt:DataEvent):void{
try{
trace( "From Server:", evt.data );
}
catch(e:Error){
trace('error');
}}
function onError(evt:IOErrorEvent):void{
trace("Connect failed");
socket.removeEventListener(Event.CONNECT, onConnect);
socket.removeEventListener(IOErrorEvent.IO_ERROR, onError);
stage.addEventListener(MouseEvent.CLICK, doConnect);
}
</fx:script>
</fx:Declarations>
First, make sure you use fx:Script with a capital S; not a lowercase S.
Then move your fx:Script tag out of the fx:Declaration and your code will compile:
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script>
var socket:XMLSocket;
stage.addEventListener(MouseEvent.CLICK, doConnect);
function doConnect(evt:Event):void{
stage.removeEventListener(MouseEvent.CLICK, doConnect);
socket = new XMLSocket("127.0.0.1", 9001);
socket.addEventListener(Event.CONNECT, onConnect);
socket.addEventListener(IOErrorEvent.IO_ERROR, onError);
}
function onConnect(evt:Event):void{
trace("Connected");
socket.removeEventListener(Event.CONNECT, onConnect);
socket.removeEventListener(IOErrorEvent.IO_ERROR, onError);
socket.addEventListener(DataEvent.DATA, onDataReceived);
socket.addEventListener(Event.CLOSE, onSocketClose);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUp);
}
function onSocketClose(evt:Event):void{
trace("Connection Closed");
stage.removeEventListener(KeyboardEvent.KEY_UP, keyUp);
socket.removeEventListener(Event.CLOSE, onSocketClose);
socket.removeEventListener(DataEvent.DATA, onDataReceived);
}
function keyUp(evt:KeyboardEvent):void{
if(evt.keyCode == 81){
socket.send("exit");
}
else{
socket.send(evt.keyCode);
}}
function onDataReceived(evt:DataEvent):void{
try{
trace( "From Server:", evt.data );
}
catch(e:Error){
trace('error');
}}
function onError(evt:IOErrorEvent):void{
trace("Connect failed");
socket.removeEventListener(Event.CONNECT, onConnect);
socket.removeEventListener(IOErrorEvent.IO_ERROR, onError);
stage.addEventListener(MouseEvent.CLICK, doConnect);
}
</fx:Script>
The fx:Declaration tag is primarily used for MXML components that are non-visual, such as validators or services. While fx:Script is clearly non-visual it is not usually embeded inside fx:Declaration.
Related
I'm trying to use a sharedObject to coordinate the movement of several objects on the stage between multiple game players. I think that I'm very close but I seem to be having a problem converting a string stored in the sharedObject into an instance of the Ball class.
My problem is in the soSync function.
I think I need to convert into an object the value of 'objectMoved' that is stored in the sharedObject. I can trace the name of the selected object, but I cannot retrieve it as an object.
How do I store the selected object in a sharedObject and then use it to coordinate motion with the other game players?
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()">
<mx:Script>
<![CDATA[
import mx.core.UIComponent;
public var nc:NetConnection;
private var connectionURL:String = "rtmp://server address...";
public var remoteSO:SharedObject;
private var checker:Ball;
public function init():void{
//Create new checkers; place them and create listeners
for (var i:int = 0; i < 3; i++){
checker = new Ball;
checker.x = 150 + (i * 110);
checker.y = 150;
checker.name = String(i);
this.addChild(checker);
checker.addEventListener(MouseEvent.MOUSE_DOWN,handleMouseDown);
}
// Establish connection to the server
nc = new NetConnection();
//Listener triggered by the NetConnection connection
nc.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
nc.connect(connectionURL);
}
private function netStatusHandler(e:NetStatusEvent):void {
if(e.info.code == "NetConnection.Connect.Success"){
createRemoteSO();
}
}
private function handleMouseDown(e:MouseEvent):void{
//Show which object has been selected
objectInfo.text = "Clicked: "+String(e.currentTarget);
e.currentTarget.startDrag();
e.currentTarget.addEventListener(MouseEvent.MOUSE_MOVE,dragging);
e.currentTarget.addEventListener(MouseEvent.MOUSE_UP,handleMouseUp)
//Update the remote shared object from this client
function dragging(e:MouseEvent):void{
objectInfo.text = "Dragging: "+String(e.currentTarget);
//Set the shared object
remoteSO.setProperty("objectMoved",e.currentTarget.name);
remoteSO.setProperty("x",e.currentTarget.x);
remoteSO.setProperty("y",e.currentTarget.y);
}
function handleMouseUp(e:MouseEvent):void{
e.currentTarget.stopDrag();
objectInfo.text = ""
e.currentTarget.removeEventListener(MouseEvent.MOUSE_MOVE,dragging)
}
}
private function createRemoteSO():void{
// Create reference to the remote shared object
remoteSO = SharedObject.getRemote("remoteSO",nc.uri,false);
remoteSO.connect(nc);
//Listener to handle changes to the remote shared object
remoteSO.addEventListener(SyncEvent.SYNC, soSync);
}
//Called when a change is made to the remote shared object by other clients
private var counter:int;
private function soSync(se:SyncEvent):void {
if(remoteSO.data.objectMoved){
this.getChildByName(remoteSO.data.objectMoved).x = remoteSO.data.x;
this.getChildByName(remoteSO.data.objectMoved).y = remoteSO.data.y;
}else{
trace("Object does not exist")
}
}
]]>
</mx:Script>
<mx:Text x="147" y="51" text="Selected object"/>
<mx:TextArea id="objectInfo" x="237" y="50"/>
</mx:Application>
Ball.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:UIComponent xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="init()">
<mx:Script>
<![CDATA[
import mx.events.FlexEvent;
private function init():void{
var child:Shape = new Shape();
child.graphics.beginFill(0xff0000, .5);
child.graphics.drawCircle(0, 0, 50);
child.graphics.endFill();
this.addChild(child);
}
]]>
</mx:Script>
</mx:UIComponent>
I think you could use getChildByName("String/ObjectName here").
So in your case...
this.getChildByName(remoteSO.data.objectMoved).y = remoteSO.data.y;
Alternatively, could you not just add the physical object itself when using setProperty as opposed to the objects name. Like so...
remoteSO.setProperty("objectMoved",e.currentTarget);
Your Ball object seems to be a DisplayObject, which are unable to be saved in a shared object as a whole. You can, however, instantiate a Ball object, read properties out of the SO and assign them to your ball. The properties in question seem to be x and y, and not objectMoved, as your ball's name will most likely be in line of instance389 which does not spell a thing if you lose the naming context, and even if not, you can only find a child by name if it still exists. So, if you have only one object of type Ball, you assign it the X and Y read from the SO, if not, you should store several balls in your SO, so you will instantiate them later and preserve all of their coordinates.
I'm not sure if that title is descriptive enough, but here is the basic issue. I have a spark.effects.Animate instance repeating n times with a listener on the EFFECT_REPEAT event. In that event's listener, I'm incrementing a variable. Logic would dictate that if the variable started at 0 and the animation was played with a repeatCount of 3, the variable would be 2 when finished. This works fine, until I focus a different tab. When doing this, on occasion, the repeat event isn't fired/handled (or the animation is just not actually doing the animation) and my count isn't correct when finished.
This is a fully functioning example built against Flex 4.5. To duplicate just click the GO button and maintain focus on the TAB. Then click it again and switch tabs and switch back. The counts don't match.
How can I fix this so that the repeat event is called consistently?
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" width="600" height="400" applicationComplete="application1_applicationCompleteHandler(event)">
<fx:Script>
<![CDATA[
import mx.controls.Image;
import mx.events.EffectEvent;
import mx.events.FlexEvent;
import spark.effects.Animate;
import spark.effects.animation.MotionPath;
import spark.effects.animation.SimpleMotionPath;
private var animate:Animate;
private var someCounter:int = 0;
protected function application1_applicationCompleteHandler(event:FlexEvent):void
{
// Create the graphic
var img:Image = new Image();
img.source = "http://www.google.com/intl/en_com/images/srpr/logo3w.png";
// Add the graphic
addElement( img );
// Create the motion path
var smp:SimpleMotionPath = new SimpleMotionPath();
smp.property = "x";
smp.valueFrom = 0;
smp.valueTo = this.width - 275;
// Add the motion path to a Vector
var paths:Vector.<MotionPath> = new Vector.<MotionPath>();
paths.push( smp );
// Create the animation
animate = new Animate();
animate.easer = null;
animate.target = img;
animate.duration = 150;
animate.repeatCount = 15;
animate.motionPaths = paths;
animate.addEventListener( EffectEvent.EFFECT_REPEAT, animate_effectRepeatHandler );
animate.addEventListener( EffectEvent.EFFECT_END, animate_effectEndHandler );
}
private function animate_effectRepeatHandler( event:EffectEvent ):void
{
lblCounter.text = someCounter.toString();
someCounter++;
}
private function animate_effectEndHandler( event:EffectEvent ):void
{
lblCounter.text = someCounter.toString(); // Sometimes doesn't equal animate.repeatCount - 1
}
protected function button1_clickHandler(event:MouseEvent):void
{
if( !animate.isPlaying )
{
someCounter = 0;
animate.play();
}
}
]]>
</fx:Script>
<mx:Label id="lblCounter" horizontalCenter="0" bottom="50" width="150" height="50" textAlign="center" />
<mx:Button horizontalCenter="0" bottom="0" label="GO" width="150" height="50" click="button1_clickHandler(event)" />
</s:Application>
I'm quite new to Flex and ActionScript, and I'm trying to do an application whose purpose is simply to be able to dynamically create rectangles on a panel: on mouse down the rectangle is created and updated on mouse move events (left button hold down), then the rectangle is achieved with mouse up.
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
width="550" height="400"
creationComplete="this.onCreationComplete(event);">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.events.FlexEvent;
import mx.graphics.SolidColor;
import spark.primitives.Graphic;
import spark.primitives.Rect;
public static const WIDTH:int = 480;
public static const HEIGHT:int = 300;
public static const BACKGROUND:int = 0xEFEFEF;
public static const CURRENT_FILL:SolidColor = new SolidColor(0x00AA00, 0.75);
protected var rectangles:ArrayCollection = null;
protected var currentRect:Rect = null;
protected var dragging:Boolean = false;
protected function onCreationComplete(event:FlexEvent):void
{
this.rectangles = new ArrayCollection();
this.sprite.graphics.beginFill(BACKGROUND);
this.sprite.graphics.drawRect(0, 0, this.sprite.width, this.sprite.height);
this.sprite.graphics.endFill();
this.sprite.addEventListener(MouseEvent.MOUSE_DOWN, this.onMouseDown);
this.sprite.addEventListener(MouseEvent.MOUSE_UP, this.onMouseUp);
}
protected function onMouseDown(event:MouseEvent):void
{
this.currentRect = new Rect();
this.currentRect.y = 10;
this.currentRect.height = this.sprite.height - (10 * 2);
this.currentRect.x = event.localX;
this.currentRect.width = 1;
this.panel.addElement(this.currentRect);
this.dragging = true;
this.sprite.addEventListener(MouseEvent.MOUSE_MOVE, this.onMouseMove);
}
protected function onMouseUp(event:MouseEvent):void
{
if (this.dragging)
{
this.sprite.removeEventListener(MouseEvent.MOUSE_MOVE, this.onMouseMove);
this.rectangles.addItem(this.currentRect);
this.dragging = false;
}
}
protected function onMouseMove(event:MouseEvent):void
{
if (this.dragging)
{
this.currentRect.width = event.localX - this.currentRect.x;
}
}
]]>
</fx:Script>
<s:Panel id="panel" x="10" y="10" title="Calendar">
<s:SpriteVisualElement id="sprite" width="{WIDTH}" height="{HEIGHT}" />
</s:Panel>
</s:WindowedApplication>
All works fine but now I want to do some actions when the user clicks (or double clicks, or what you want) on a rectangle. I tried to add an event on each rectangle (this.currentRect.addEventListener(MouseEvent.DOUBLE_CLICK, myMethod) added in onMouseUp), but the corresponding method is never executed...
What's the problem?
Objects drawn with the graphics API are not event dispatchers. So use addElement to add the Spark primitive rects that you're creating but not using in any way (to a container such a Group) instead of using the graphics api to draw the shapes.
You might find it's better, however, to simply have the ArrayCollection contain a bunch of bindable data objects and use a DataGroup with itemRenderers to display the data.
You could keep track of the rectangles you have drawn in a collection and do a hit test with the rectangles on mouse clicks and determine if any rectangle is being clicked on.
im developing a simple Mxml application using flex 3. I have used simple text and combo box.
combobox contains items left right up and down while i click each element in combo box the scrolling text will scroll in selected direction it is working fine.
my question is how i can modify this application by pressing key board Arrow keys up, down, right and left. instead of using combobox elements??
my application code:
<?xml version="1.0" encoding="utf-8"?>
<mx:Script>
<![CDATA[
public var mytimer:Timer=new Timer(10);
[Bindable]public var arr:Array=new Array("upScroll","LeftScroll","right","down");
private function initApp():void
{
mytimer.start();
mytimer.addEventListener(TimerEvent.TIMER,scrollme);
}
private function scrollme(event:TimerEvent):void
{
if(cmb.selectedLabel=="LeftScroll")
{
if(mytext.x==0)
mytext.x=this.width-mytext.width;
mytext.x--;
}
if(cmb.selectedLabel=="upScroll")
{
if(mytext.y==0)
mytext.y=600;
mytext.y--;
}
if(cmb.selectedLabel=="right")
{
if(mytext.x==this.width-mytext.width)
mytext.x=0;
mytext.x++;
}
if(cmb.selectedLabel=="down")
{
if(mytext.y==600)
mytext.y=0;
mytext.y++;
}
}
]]>
</mx:Script>
<mx:Text id="mytext" text="SCROLLING" fontSize="16" fontStyle="italic" fontWeight="bold"/>
<mx:ComboBox dataProvider="{arr}" prompt="Select" id="cmb" change="initApp()"/>
</mx:Application>
You can subscribe to Keyboard events of an Application:
<s:Application
...
creationComplete="creactionComplete()">
function creactionComplete():void
{
addEventListener(KeyboardEvent.KEY_DOWN, keyHitHandler);
}
private function keyHitHandler(event:KeyboardEvent):void
{
if(event.keyCode == Keyboard.DOWN)
//...
if(event.keyCode == Keyboard.UP)
//...
}
How can I prevent CTRL+A from functioning with editable TextField()
The previous example only works with Flex Text and TextArea objects, this works with all flash.text.* objects.
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()">
<mx:Script>
<![CDATA[
import mx.core.UIComponent;
private var t:TextField;
private function init():void
{
t = new TextField();
t.height = 80;
t.width = 100;
t.type = TextFieldType.INPUT;
t.multiline = true;
var c:UIComponent = new UIComponent();
c.addChild( t );
foo.addChild( c );
addEventListener( KeyboardEvent.KEY_UP, edit );
addEventListener( KeyboardEvent.KEY_DOWN, edit );
}
private function edit( event:KeyboardEvent ):void
{
if( event.type == KeyboardEvent.KEY_DOWN && event.ctrlKey )
{
t.type = TextFieldType.DYNAMIC; // Dynamic texts cannot be edited. You might be able to remove this line.
t.selectable = false; // If selectable is false, then Ctrl-a won't do anything.
}
else
{
t.type = TextFieldType.INPUT;
t.selectable = true;
}
}
]]>
</mx:Script>
<mx:Canvas id="foo" height="90" width="110" backgroundColor="#FFFFFF" />
</mx:Application>
Not tested, but perhaps you could catch the selectAll event on the TextField and prevent it bubbling up, or clear the selection (not sure when the event is fired).
Use the setFocus function paired with a KeyboardEvent listener:
<xml version="1.0"?>
<!-- menus/SimpleMenuControl.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" width="800" initialize="init()" >
<mx:TextInput id="nom"/>
<mx:Script>
<![CDATA[
private function init():void
{
addEventListener( KeyboardEvent.KEY_UP, edit );
addEventListener( KeyboardEvent.KEY_DOWN, edit );
}
private function edit( event:KeyboardEvent ):void
{
if( event.type == KeyboardEvent.KEY_DOWN && event.ctrlKey ) setFocus();
else nom.setFocus();
nom.selectionEndIndex = nom.selectionBeginIndex = nom.text.length;
}
]]>
</mx:Script>
</mx:Application>
The setFocus means that the Text object will no longer listen to any keyboard events.
I would not recommend using the enabled property as that will gray-out the textarea.