Increase the gap between the buttons of Alert Box in Flex 3 - actionscript-3

I am trying to put a gap of 20-30px between the Alert box buttons(YES and NO).
but unable to find such styling point in flex. I have tried horizontal-gap, and also padding, but in vain.
Below is the sample code i am trying, which i found when browsing through sites.
<?xml version="1.0" encoding="utf-8"?>
<mx:Application name="Alert_style_test"
xmlns:mx="http://www.adobe.com/2006/mxml"
layout="vertical"
verticalAlign="middle"
backgroundColor="white"
creationComplete="showAlert()">
<!-- Used by the Alert control. -->
<mx:String id="message">The quick brown fox jumped over the lazy dog.
The quick brown fox jumped over the lazy dog.</mx:String>
<mx:String id="title">The quick brown fox jumped over the lazy dog?</mx:String>
<mx:Script>
<![CDATA[
import mx.controls.Alert;
private var a:Alert;
private function showAlert():void {
Alert.yesLabel = "Yes";
Alert.noLabel = "No";
Alert.buttonWidth = 50;
a = Alert.show(
message,
title,
Alert.NO | Alert.YES
);
/* Make the Alert form's text non-selectable. */
a.mx_internal::alertForm.mx_internal::textField.selectable = false;
}
]]>
</mx:Script>
<mx:Style>
Alert{
color : #124332;
background-color: #ffffff;
header-colors : #243322, #243322;
header-height:19;
drop-shadow-enabled: true;
drop-shadow-color :#243322;
corner-radius :6;
border-style :solid;
border-thickness: 1;
border-color : #243322;
footer-colors : #243322, #ffffff;
title-style-name : "title";
horizontal-gap:500;
horizontal-separator-skin:white;
}
.title{
font-family :Verdana;
font-size :10;
font-weight :bold;
color :#ffffff;
}
.alertButton {
letterSpacing: 0;
fontSize: 11;
cornerRadius: 10;
fontWeight: normal;
textRollOverColor: white;
color: red;
horizontal-gap:-500;
}
</mx:Style>
<!-- Click to launch Alert control. -->
<mx:Button label="Launch Alert" click="showAlert();" />
</mx:Application>

Try something like this:
Add FlexEvent.UPDATE_COMPLETE to alertForm in your alert:
a.mx_internal::alertForm.addEventListener(FlexEvent.UPDATE_COMPLETE, alertForm_updateHandler);
And in this handler copy some stuff from original alertForm updateDisplayList method:
private function alertForm_updateHandler(event:FlexEvent):void
{
var form:UIComponent = a.mx_internal::alertForm;
var buttons:Array = a.mx_internal::alertForm.mx_internal::buttons;
var newX:Number;
var newY:Number;
var newWidth:Number;
newWidth = buttons.length * (buttons[0].width + 120) - 120;
newX = Math.round((form.width - newWidth) / 2);
for (var i:int = 0; i < buttons.length; i++)
{
buttons[i].x = newX
buttons[i].tabIndex = i + 1;
newX += buttons[i].width + 120;
}
}
where 120 is your new gap.
Hope this can be useful.

The only idea I have is to implement a new class as a child of TitleWindow or Panel and to add all the features you want to have. I know it is not the best solution, but you can try it.
Here are my proposals:
//Application
<?xml version="1.0" encoding="utf-8"?>
<mx: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:Script>
<![CDATA[
import mx.events.CloseEvent;
private function onBtnClick():void
{
var title:String = "The quick brown fox jumped over the lazy dog?";
var message:String = "The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog.";
AdvancedAlert.buttonWidth = 70;
AdvancedAlert.buttonGap = 50;
AdvancedAlert.commonWidth = 400;
AdvancedAlert.show(message, title, AdvancedAlert.YES | AdvancedAlert.NO | AdvancedAlert.CANCEL, this._closeHandler);
}
private function _closeHandler(evt:CloseEvent):void
{
switch (evt.detail)
{
case AdvancedAlert.YES:
trace("yes");
break;
case AdvancedAlert.NO:
trace("no");
break;
case AdvancedAlert.OK:
trace("ok");
break;
case AdvancedAlert.CANCEL:
trace("cancel");
break;
}
}
]]>
</fx:Script>
<mx:Button click="onBtnClick()" label="Alert"/>
</mx:Application>
//Alert implementation
<?xml version="1.0" encoding="utf-8"?>
<mx:TitleWindow xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:mx="library://ns.adobe.com/flex/mx"
layout="absolute"
width="{commonWidth}"
height="110"
styleName="titleWindow"
verticalScrollPolicy="off"
horizontalScrollPolicy="off"
creationComplete="onCreationComplete()" >
<fx:Style>
#namespace mx "library://ns.adobe.com/flex/mx";
.windowStyles {
color: #ffffff;
}
.titleWindow {
borderAlpha: 1.0;
borderColor: #8a9faa;
backgroundColor: #8a9faa;
cornerRadius: 5;
}
</fx:Style>
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.controls.Button;
import mx.core.FlexGlobals;
import mx.events.CloseEvent;
import mx.managers.PopUpManager;
public static const YES:uint = 0x0001;
public static const NO:uint = 0x0002;
public static const OK:uint = 0x0004;
public static const CANCEL:uint= 0x0008;
[Bindable]private var message:String;
[Bindable]public static var buttonGap:int = 20;
public static var buttonWidth:int = 80;
[Bindable]public static var commonWidth:int = 80;
[Bindable]private var buttonHeight:int = 25;
private var buttons:uint;
private var buttonNames:ArrayCollection = new ArrayCollection();
private function onCreationComplete():void
{
addButtons();
}
private function addButtons():void
{
buttonNames.removeAll();
hbButtons.removeAllChildren();
if ((YES & buttons) != 0)
buttonNames.addItem("Yes");
if ((NO & buttons) != 0)
buttonNames.addItem("No");
if ((OK & buttons) != 0)
buttonNames.addItem("Ok");
if ((CANCEL & buttons) != 0)
buttonNames.addItem("Cancel");
for each (var bn:String in buttonNames)
{
var btn:Button = new Button();
btn.width = buttonWidth;
btn.height = buttonHeight;
btn.label = bn;
btn.name = bn;
btn.addEventListener(MouseEvent.CLICK, onBtnClick);
hbButtons.addChild(btn);
}
}
private function onBtnClick(evt:Event):void
{
var currentButtonName:String = (evt.currentTarget as Button).name;
var closeEvent:CloseEvent = new CloseEvent(CloseEvent.CLOSE);
switch (currentButtonName)
{
case "Yes":
closeEvent.detail = YES;
break;
case "No":
closeEvent.detail = NO;
break;
case "Ok":
closeEvent.detail = OK;
break;
case "Cancel":
closeEvent.detail = CANCEL;
break;
}
this.dispatchEvent(closeEvent);
PopUpManager.removePopUp(this);
}
public static function show(message:String = "", title:String = "", buttons:uint = 0x4, closeHandler:Function = null):void
{
var advancedAlert:AdvancedAlert = new AdvancedAlert();
advancedAlert.message = message;
advancedAlert.title = title;
advancedAlert.buttons = buttons;
if (closeHandler != null)
advancedAlert.addEventListener(CloseEvent.CLOSE, closeHandler);
PopUpManager.addPopUp(advancedAlert, Sprite(FlexGlobals.topLevelApplication), true);
PopUpManager.centerPopUp(advancedAlert);
}
]]>
</fx:Script>
<mx:VBox id="vbMain" width="100%" height="100%">
<mx:Text id="txtMessage" text="{message}" width="100%" height="35" color="0xffffff" selectable="false"/>
<mx:HBox id="hbButtons" height="{buttonHeight}" width="100%" horizontalGap="{buttonGap}" horizontalAlign="center"/>
</mx:VBox>
</mx:TitleWindow>

Related

AS3 Extended Jump when Holding Button

So my mario project must include a staple of Mario's movement, and that of course is the option to jump a short height or a fairly large one. As we all know, holding down the jump button makes him jump higher, that's what my goal is here. In my case, that button is X and I am unsure of how to do that.
This is currently my unsuccessful attempt, and gravity is set to 0.87 by default in my variables.
This is in my keyDownHandler (when the key is pressed)
if (event.keyCode == Keyboard.X && onGround == true)
{
vy += jumpForce;
holdJump = true;
onGround = false;
if(holdJump == true && onGround == false)
{
_mario.y += 1;
}
else
{
vy = vy + (grav * 0.20);
holdJump = false;
}
This is in my keyUpHandler (when the key is not pressed/let go)
if (event.keyCode == Keyboard.X)
{
if (holdJump == false)
{
accy = 0;
gravity = 0.80;
incSpeedY = 0;
}
}
Ok, I've extended my comment.
You can use standard vy=vyLast-g*(t-tLast), and just set vyLast to min(0,vyLast) when jump key is released, and set it to jump starting speed when jump key is pressed on the ground.
Here is the sample Adobe Air application with jumping red circle. It implements the logics that I've described into the comment:
<?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"
>
<fx:Script>
<![CDATA[
import flash.utils.getTimer;
import mx.graphics.SolidColor;
public var marioY:Number = 0; //jump height above ground (meters)
public var g:Number = -9.81; //meter/(second^2)
public var lastTime:Number = NaN;
public var lastVerticalSpeed:Number = 0;//speed of a flight -meters/second
public var jumpSpeed:Number = 10;//initial jump speed - meters/second
public var timeRatio:Number = 1000;//milliseconds in a second
public var heightRatio:Number = 50; //pixels/meter
protected function get landed():Boolean{
return marioY <= 0 && lastVerticalSpeed <= 0;
}
protected function onKeyDown(event:KeyboardEvent):void{
if(event.keyCode==Keyboard.UP && landed){
lastVerticalSpeed = jumpSpeed;
trace('fly!');
}
}
protected function onKeyUp(event:KeyboardEvent):void{
if(event.keyCode==Keyboard.UP){
lastVerticalSpeed = Math.min(0,lastVerticalSpeed);
trace('fall!');
}
}
protected function onEnterFrame(event:Event):void{
if(!isNaN(lastTime)){
var deltaTime:Number = (getTimer() - lastTime)/timeRatio;
marioY+=lastVerticalSpeed*deltaTime;
if(landed){
lastVerticalSpeed=0;
marioY=0;
}else{
lastVerticalSpeed+=g*deltaTime;
}
}
mario.y=area.height-marioY*heightRatio-20;
lastTime = getTimer();
}
]]>
</fx:Script>
<s:Group width="100%" height="100%" keyDown="onKeyDown(event)" keyUp="onKeyUp(event)"
enterFrame="onEnterFrame(event)" id="area"
creationComplete="area.setFocus()"
>
<s:Rect width="100%" height="100%" fill="{new SolidColor(0x0000FF)}"/>
<s:Ellipse id="mario" width="10" height="10" fill="{new SolidColor(0xFF0000)}"
y="100" x="100"
/>
</s:Group>
</s:WindowedApplication>

IconItemRenderer with HTML Support

Im currently developing an custom Icon Item Renderer that support HTML in the message fields.
I have two files.
view1.mxml - that contains the spak list component
htmlRenderer.mxml - the icon item renderer
Codes
htmlRenderer.mxml
<?xml version="1.0" encoding="utf-8"?>
<s:ItemRenderer
xmlns:fx = "http://ns.adobe.com/mxml/2009"
xmlns:s = "library://ns.adobe.com/flex/spark"
xmlns:mx = "library://ns.adobe.com/flex/mx"
autoDrawBackground = "true"
width = "100%"
creationComplete="callLater(renderHtml, ['test'])">
<fx:Script>
<![CDATA[
import flash.display.Shape;
import mx.core.FlexGlobals;
import mx.core.UIComponent;
import mx.events.FlexEvent;
import spark.components.*;
import spark.components.supportClasses.StyleableTextField;
import spark.primitives.Rect;
public var mainWrapper:VGroup = new VGroup();
public var mainContainer:HGroup = new HGroup();
//Icon Item Renderer
public var iconItemWrapper:HGroup = new HGroup();
public var iconItemImage:Image = new Image();
/* Text elements */
public var iconItemTextGroup:VGroup = new VGroup();
public var iconItemLabel:Label = new Label();
public var iconItemHtmlMessagex:TextArea = new TextArea();
/* Star rating */
public var iconItemRaterGroup:HGroup = new HGroup();
public var iconItemRater:*;
//Decorator
public var decoratorGroup:VGroup = new VGroup();
public var decoratorText:Label = new Label();
public var counterGroup:Rect = new Rect();
public var counterText:Label = new Label();
public var decoratorImage:Image = new Image();
//Icon Item Borders
public var iconItemTopBorder:Rect = new Rect();
public var iconItemBottomBorder:Rect = new Rect();
public var iconItemBackground:Rect = new Rect();
import flash.display.Graphics;
import mx.graphics.LinearGradient;
import mx.graphics.GradientEntry;
protected function drawHeader():void
{
decoratorText.setStyle("fontFamily","Roboto");
decoratorText.setStyle("fontSize","16");
decoratorText.setStyle("fontWeight","bold");
decoratorText.setStyle("color","#ffffff");
decoratorText.setStyle("paddingRight","5");
decoratorText.setStyle("paddingTop","5");
decoratorText.setStyle("paddingLeft","5");
decoratorText.setStyle("paddingBottom","5");
decoratorText.text = data.Location
addElement(decoratorText);
}
protected function renderIconItem():void
{
//main wrapper
iconItemWrapper.paddingBottom=5;
iconItemWrapper.paddingTop=5;
iconItemWrapper.paddingLeft=5;
iconItemWrapper.verticalAlign="top";
addElement(iconItemWrapper);
//icon
iconItemImage.source = data.Image;
if(iconItemImage.sourceWidth > 64){
iconItemImage.width = 64;
iconItemImage.height = 64;
}else{
iconItemImage.width = iconItemImage.sourceWidth;
iconItemImage.height = iconItemImage.sourceHeight;
}
iconItemImage.sourceWidth
iconItemWrapper.addElement(iconItemImage);
iconItemTextGroup.gap = 0;
iconItemTextGroup.paddingBottom=0;
iconItemTextGroup.paddingTop=0;
iconItemTextGroup.verticalAlign="top";
iconItemWrapper.addElement(iconItemTextGroup);
//title
iconItemLabel.setStyle("fontFamily","Roboto");
iconItemLabel.setStyle("fontWeight","bold");
iconItemLabel.setStyle("color","#000000");
iconItemLabel.setStyle("fontSize","16");
iconItemLabel.setStyle("paddingRight","0");
iconItemLabel.setStyle("paddingTop","0");
iconItemLabel.setStyle("paddingLeft","0");
iconItemLabel.setStyle("paddingBottom","0");
iconItemLabel.text = data.Product;
iconItemTextGroup.addElement(iconItemLabel);
//message
iconItemHtmlMessagex.focusEnabled = false;
iconItemHtmlMessagex.selectable = false;
iconItemHtmlMessagex.setStyle("paddingLeft","0");
iconItemHtmlMessagex.setStyle("paddingTop","0");
iconItemHtmlMessagex.setStyle("borderVisible","false");
iconItemHtmlMessagex.setStyle("contentBackgroundAlpha","0");
iconItemTextGroup.addElement(iconItemHtmlMessagex);
renderMessageText();
//iconItemRaterGroup
//iconItemRaterGroup.paddingTop=0;
//iconItemRaterGroup.verticalAlign="bottom";
//iconItemTextGroup.addElement(iconItemRaterGroup);
//decoratorGroup
decoratorGroup.paddingTop=10;
decoratorGroup.verticalAlign="bottom";
iconItemWrapper.addElement(decoratorGroup);
//decoratorText
decoratorText.setStyle("fontFamily","Roboto");
decoratorText.setStyle("fontSize","12");
decoratorText.setStyle("fontWeight","bold");
decoratorText.setStyle("color","#777777");
decoratorText.setStyle("paddingRight","0");
decoratorText.setStyle("paddingTop","0");
decoratorText.setStyle("paddingLeft","0");
decoratorText.setStyle("paddingBottom","0");
decoratorText.text = data.Location
decoratorGroup.addElement(decoratorText);
//decoratorImage
decoratorImage.width = 32;
decoratorImage.height = 32;
decoratorImage.source = "recycle-icon.png";
decoratorImage.sourceHeight
decoratorImage.sourceWidth
decoratorGroup.addElement(decoratorImage);
}
public var myStyleSheet:StyleSheet = new StyleSheet();
private function renderMessageText():void {
var styles:String = "p{ font-size: 11px; }
a{ font-size: 11px; color: #0C81F5; text-decoration: underline; }
a:hover { color: #CCDEF0; text-decoration: underline; }";
myStyleSheet.parseCSS(styles);
StyleableTextField(iconItemHtmlMessagex.textDisplay).htmlText = data.Description2;
}
public function renderHtml(varx:String):void{
setTimeout(renderHtmlTimeout, 1);
}
public function renderHtmlTimeout():void{
StyleableTextField(iconItemHtmlMessagex.textDisplay).styleSheet = myStyleSheet;
}
override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
{
super.updateDisplayList(unscaledWidth, unscaledHeight);
renderIconItem();
this.graphics.clear();
this.graphics.beginGradientFill(GradientType.LINEAR, [0xffffff,0xefefef], [1,1], [0,255],verticalGradientMatrix(0,0,unscaledWidth, unscaledHeight));
this.graphics.drawRect(0,0,unscaledWidth, unscaledHeight);
}
]]>
</fx:Script>
</s:ItemRenderer>
and view1.mxml
<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
creationComplete="ini();">
<fx:Declarations>
<s:HTTPService id="xmlDataResource" url="properties.xml"
result="xmlDatasource = xmlDataResource.lastResult.slist.products"/>
</fx:Declarations>
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
[Bindable]
public var xmlDatasource:ArrayCollection;
public function ini():void{
xmlDataResource.send();
}
]]>
</fx:Script>
<s:List id="categoryList" left="0" right="0" top="0" bottom="0" borderAlpha="0.5"
itemRenderer="htmlRenderer"
dataProvider="{xmlDatasource}">
</s:List>
</s:View>
The problem is that when the list loads data, only list items in the view port are rendered and the rest of the information is hidden.
Any help as im in the verge of getting a breakthrough in flex mobile.
below is the screen shot
thank
Basically, the rest is not hidden, it is simply not there :)
The concept of the List-based classes/components in Flex is, the only the visible data is assigned and rendered. If e.g. your list has the height to display 8 items, then it will create 10 items and reuse them. If you scroll up and an items leaves the viewport, it is placed at the bottom of the list and gets new data.
If you want to create all objects and scroll them, try wrapping a VGroup inside a Scroller.

Cannot correctly resize an image via action script

Hi I am trying to add an image when I click on a thumbnail.
I know I have to use a listener (Event.COMPLETE ?), but the image is not resizing correctly when I rotate the tablet.
I believe the problem is that I cannot resize the image within the addImage1() function, as the image has not yet loaded, but I cannot use newImg.width within the listener function to reset the image width.
Any help would be most appreciated.
Code follows:-)
<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark" title="ZOOM Pictues with Tap"
resize="view1_resizeHandler(event)">
<s:layout>
<s:VerticalLayout/>
</s:layout>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script>
<![CDATA[
import mx.events.FlexEvent;
import mx.events.ResizeEvent;
public var hasImageBeenAdded:Boolean = false;
private var imageLastWidth:Number;
private var imageLastHeight:Number;
private var zoomFactor:Number;
private var imageNumber:Number;
private var rhsKeepWidth:int;
private var rhsKeepHeight:int;
private var rhsKeep:Boolean = false;
protected function view1_resizeHandler(event:ResizeEvent):void
{
if(rhsKeepWidth > rhsKeepHeight) // Was Landscape is now Portrait
{
var tmpWidth2:int = rhsKeepWidth;
var tmpHeight2:int = rhsKeepHeight;
rhsKeepWidth = tmpHeight2-lhs.width;
rhsKeepHeight = tmpWidth2+lhs.width;
}
else //Was Portrait is now Landscape
{
var tmpWidth1:int = rhsKeepWidth;
var tmpHeight1:int = rhsKeepHeight;
rhsKeepWidth = tmpHeight1-lhs.width;
rhsKeepHeight = tmpWidth1+lhs.width;
}
addImage1();
}
protected function removeAllImages():void
{
var totalElements : int = rhs.numElements;
for(var i:int=totalElements-1;i>=0;i--)
{
rhs.removeElementAt(i);
}
}
private function onImageLoaded(e:Event):void
{
var zoomFactor1:Number;
var zoomFactor2:Number;
imageLastWidth = e.target.sourceWidth;
imageLastHeight = e.target.sourceHeight;
if(rhsKeep != true) //Need to set the rhs VGroup dimensions
{
rhs.width = hGroup1.width-lhs.width;
rhs.height = hGroup1.height;
rhsKeep = true;
rhsKeepWidth = rhs.width;
rhsKeepHeight = rhs.height;
}
zoomFactor1 = rhsKeepWidth/imageLastWidth;
zoomFactor2 = rhsKeepHeight/imageLastHeight;
if(zoomFactor1 < zoomFactor2)
{
zoomFactor = zoomFactor1;
}
else
{
zoomFactor = zoomFactor2;
}
trace("zoomFactor=" + zoomFactor);
}
public function addImage1():void
{
var i:int;
var newImg:Image = new Image();
removeAllImages();
newImg.source = "images/1.jpg";
newImg.x = 0;
newImg.y = 0;
newImg.addEventListener(Event.COMPLETE,onImageLoaded);
rhs.addElementAt(newImg,0);
hasImageBeenAdded = true;
imageNumber = 1;
trace("Image Width= " + newImg.width);
newImg.scaleX = newImg.scaleY = zoomFactor;
}
]]>
</fx:Script>
<s:HGroup id="hGroup1" width="100%" height="100%">
<s:Scroller id="scrollerL" height="100%">
<s:VGroup id="lhs" width="15%" height="100%" gap="10"
horizontalAlign="center" verticalAlign="top">
<s:Image width="100" height="100" source="thumbs/1.jpg" click="addImage1()"/>
</s:VGroup>
</s:Scroller>
<s:Scroller id="scroller1" height="100%" width="100%">
<s:VGroup id="rhs" height="100%">
</s:VGroup>
</s:Scroller>
</s:HGroup>
</s:View>
Regarding the device orientation, you should use:
trace(Stage.deviceOrientation);
trace(Stage.orientation);
They are read-only strings outputting information regarding the device's and the screen's orientation. You may manually set the stage's orientation:
Stage.setOrientation(StageOrientation.DEFAULT);
Regarding the image loading, you need an eventListener for Event.COMPLETE and you should also cast the content as Image:
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, function(event:Event):void {
trace("Image loaded");
// Handle code here
// Use: (loader.content as Image)
});
loader.load(new URLRequest("images/1.jpg"));

rummy card placement on canvas

I am trying to place 5 cards overlapping 30% on one another. and trying to give a movement to it using mouse events . It should drop within the 5 cards only, not outside of that.
I have learned the drag and drop events and executed it, but i cannot place the card within the 5 cards .
Please somebody help me in this. Please Check the Below Code:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="carcan();">
<mx:Script>
<![CDATA[
import mx.containers.Canvas;
import mx.controls.Image;
private var images:Array;
private var images1:Array;
private const IMAGE_COUNT:uint = 5;
private var img:Image;
private var img1:Image;
private var points:flash.geom.Point;
private var offset_x:int;
private var offset_y:int;
private var canal:Canvas;
private var doDrag:Boolean;
[Embed(source='cards/2C.png')]
private var Image0:Class;
[Embed(source='cards/2D.png')]
private var Image1:Class;
[Embed(source='cards/2H.png')]
private var Image2:Class;
[Embed(source='cards/2S.png')]
private var Image3:Class;
[Embed(source='cards/3C.png')]
private var Image4:Class;
public function carcan():void
{
canal = new Canvas();
canal.setStyle("backgroundColor","blue");
canal.x=100;
canal.y=50;
canal.width=500;
canal.height=400;
this.addChild(canal);
init();
}
public function init():void
{
images = new Array(IMAGE_COUNT);
for (var i:int = 0; i < IMAGE_COUNT; i++)
{
img= new Image();
img1= new Image();
images[i] = this["Image" + i];
trace(images[i]);
img.x=(i*30)+50;
img.source=images[i];
img.id="Image"+i;
canal.addChild(img);
img.addEventListener(MouseEvent.MOUSE_DOWN, md);
img.addEventListener(MouseEvent.MOUSE_MOVE, mm);
canal.addEventListener(MouseEvent.MOUSE_OUT,smu);
img.addEventListener(MouseEvent.MOUSE_UP, mu);
}
}
public function smu(event:MouseEvent):void
{
img.alpha=1;
img.stopDrag();
doDrag=false;
setCards();
}
public function mo(event:MouseEvent):void
{
if(doDrag==true)
{
img.addEventListener(MouseEvent.MOUSE_DOWN, md);
img.addEventListener(MouseEvent.MOUSE_UP, mu);
img.stopDrag();
img.alpha=1;
img.removeEventListener(MouseEvent.MOUSE_MOVE, mm);
}
else
{
img.addEventListener(MouseEvent.MOUSE_MOVE, mm);
}
}
public function md(event:MouseEvent):void
{
img = new Image();
doDrag=true;
canal.setChildIndex(Image(event.target),images.length-1);
img.addEventListener(MouseEvent.MOUSE_MOVE, mm);
}
public function mm(event:MouseEvent):void
{
if(doDrag==true)
{
points = new Point();
images = new Array(IMAGE_COUNT);
img = new Image();
img = Image(event.target);
points.x=event.target.x;
points.y=event.target.y;
points = localToGlobal(points);
img.x=points.x;
img.y=points.y;
img.alpha=0.7;
img.addEventListener(MouseEvent.MOUSE_UP, mu);
var boundar:flash.geom.Rectangle = new Rectangle(this.x, this.y, 250, 100);
}
}
public function mu(event:MouseEvent):void
{
img.alpha=1;
canal.stopDrag();
doDrag=false;
canal.stopDrag();
doDrag=false;
var current:Image = event.currentTarget as Image;
var num1:int = current.x;
if(num1 < 50){
canal.setChildIndex(current, images.length-5);
current.y=0;
setCards();
}
if(num1 > 50 && num1 < 80){
canal.setChildIndex(current, images.length-4);
current.y=0;
setCards();
}
if(num1 > 80 && num1 < 110){
canal.setChildIndex(current, images.length-3);
current.y=0;
setCards();
}
if(num1 > 110 && num1 < 140){
canal.setChildIndex(current, images.length-2);
current.y=0;
setCards();
}
if(num1 > 140 && num1 < 170){
canal.setChildIndex(current, images.length-2);
current.y=0;
setCards();
}
if(num1 > 170){
canal.setChildIndex(current, images.length-1);
current.y=0;
setCards();
}
}
private function setCards():void{
var b:int = 0;
var a:int;
var cardsArray:Array = canal.getChildren();
for(a = 0;a < cardsArray.length; a++)
{
canal.getChildAt(a).x = 50+b;
b=b+30;
canal.getChildAt(a).y=0;
}
}
]]>
</mx:Script>
</mx:Application>
PS: I am trying to replace the drag and drop events with mouse events and get the same functionality using mouse events. Please somebody help me in this.
Hope Below Code may help you: - You can use Drag and drop as per below code and create it in AS. I have created it in MXML which will give you some idea what you are looking for: -
<?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" xmlns:local="*">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script>
<![CDATA[
import mx.core.DragSource;
import mx.core.UIComponent;
import mx.events.DragEvent;
import mx.managers.DragManager;
private function doDragEnter(event:DragEvent):void
{
DragManager.acceptDragDrop(UIComponent(event.target));
}
private function doDragDrop(event:DragEvent):void
{
var img:RummyItemRenderer;
if (event.dragInitiator.parent == mainCanvas)
{
img = event.dragInitiator as RummyItemRenderer;
//mainCanvas.swapChildren(img, event.currentTarget as RummyItemRenderer);
var index:Number = mainCanvas.getChildIndex(event.currentTarget as RummyItemRenderer);
mainCanvas.removeChild(img);
mainCanvas.addChildAt(img,index);
setCardsPosition();
}
}
private function setCardsPosition():void{
var b:int = 0;
var a:int;
var cardsArray:Array = mainCanvas.getChildren();
for(a = 0;a < cardsArray.length; a++)
{
mainCanvas.getChildAt(a).x = 50+b;
b = b+30;
mainCanvas.getChildAt(a).y=20;
}
}
private function doDragStart(event:MouseEvent):void
{
var dragInitiator:RummyItemRenderer = event.currentTarget as RummyItemRenderer;
var dragSource:DragSource = new DragSource();
DragManager.doDrag(dragInitiator, dragSource, event);
}
]]>
</fx:Script>
<mx:Canvas id="mainCanvas" backgroundColor="#DDDDDD" width="400" height="200" x="50" y="50">
<local:RummyItemRenderer id="firstID" x="{mainCanvas.x}" y="20" width="200" height="80%" backgroundColor="#FF0000"
mouseDown="doDragStart(event)" dragEnter="doDragEnter(event)" dragDrop="doDragDrop(event)"
setImageSource="myImageURL"/>
<local:RummyItemRenderer id="secondID" x="{mainCanvas.x + 30}" y="20" width="200" height="80%" backgroundColor="#00FF00"
mouseDown="doDragStart(event)" dragEnter="doDragEnter(event)" dragDrop="doDragDrop(event)"
setImageSource="myImageURL"/>
<local:RummyItemRenderer id="thirdID" x="{mainCanvas.x + 60}" y="20" width="200" height="80%" backgroundColor="#0000FF"
mouseDown="doDragStart(event)" dragEnter="doDragEnter(event)" dragDrop="doDragDrop(event)"
setImageSource="myImageURL"/>
</mx:Canvas>
</s:Application>
RummyItemRenderer.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:Canvas 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="100%" height="100%">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script>
<![CDATA[
[Bindable]
private var _setImageSource:String;
public function get setImageSource():String
{
return _setImageSource;
}
public function set setImageSource(sourceURL:String):void
{
_setImageSource = sourceURL;
}
]]>
</fx:Script>
<s:Image id="imageID" source="{_setImageSource}"/>
</mx:Canvas>

Adobe Flex creating polygon

How to create a polygon in Adobe flex 3.0
You draw a bunch of lines connecting the points of the polygon.
As a quick example:
function drawPolygon(first, ... rest) {
graphics.moveTo(first.x, first.y);
for(var i = 0; i < rest.length; i++) {
graphics.lineTo(rest[i].x, rest[i].y);
}
graphics.lineTo(first.x, first.y);
}
May be some minor syntax errors, but you get the idea. You'd call it by passing a bunch of Point objects indicating the points of the polygon.
Try this sample
<?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"
xmlns:esri="http://www.esri.com/2008/ags">
<fx:Script>
<![CDATA[
import com.esri.ags.Graphic;
import com.esri.ags.SpatialReference;
import com.esri.ags.Units;
import com.esri.ags.geometry.Geometry;
import com.esri.ags.geometry.MapPoint;
import com.esri.ags.geometry.Polygon;
import com.esri.ags.geometry.Polyline;
import com.esri.ags.utils.GeometryUtil;
import mx.utils.StringUtil;
private const sr:SpatialReference = new SpatialReference(4326);
protected function onCreatePolyline(event:MouseEvent):void
{
addMessage("Create polyline clicked");
var pts:Array = new Array();
for (var i:int; i < 10; i++) // add 10 random points to path
{
var pt:MapPoint = new MapPoint(Math.random()*10000, Math.random()*10000, sr);
pts.push(pt);
}
var pl:Polyline = new Polyline(new Array(pts), sr);
var lengths:Array = GeometryUtil.geodesicLengths(new Array(pl), Units.KILOMETERS);
if (lengths != null && lengths.length > 0)
{
addMessage(StringUtil.substitute("polyline created with length {0} km", lengths[0]));
}
addGraphic(pl);
}
protected function onCreatePolygon(event:MouseEvent):void
{
addMessage("Create polygon clicked");
var pts:Array = new Array();
for (var i:int; i < 10; i++) // add 10 random points to ring
{
var pt:MapPoint = new MapPoint(Math.random()*10000, Math.random()*10000, sr);
pts.push(pt);
}
var pg:Polygon = new Polygon(new Array(pts), sr);
var areas:Array = GeometryUtil.geodesicAreas(new Array(pg), Units.SQUARE_KILOMETERS);
if (areas != null && areas.length > 0)
{
addMessage(StringUtil.substitute("polygon created with area {0} kmĀ²", Math.abs(areas[0])));
}
addGraphic(pg);
}
private function addMessage(message:String):void
{
log.text = StringUtil.substitute("> > > {0}\n{1}", message, log.text);
}
private function addGraphic(geometry:Geometry):void
{
var gr:Graphic = new Graphic(geometry);
grLayer.clear();
var grId:String = grLayer.add(gr);
addMessage(StringUtil.substitute("graphic added with id='{0}'", grId));
map.initialExtent = geometry.extent;
map.zoomToInitialExtent();
}
]]>
</fx:Script>
<s:layout>
<s:VerticalLayout gap="10"
paddingBottom="10"
paddingLeft="10"
paddingRight="10"
paddingTop="10"/>
</s:layout>
<s:Button label="Create polyline"
click="onCreatePolyline(event)"/>
<s:Button label="Create polygon"
click="onCreatePolygon(event)"/>
<s:TextArea id="log"
width="100%"
height="100%"/>
<esri:Map id="map"
zoomSliderVisible="false"
minHeight="200"
width="100%">
<esri:GraphicsLayer id="grLayer" />
</esri:Map>
</s:Application>