国庆60周年倒计时牌(4 位领导批示)
- 2009-09-2
- 分类:ActionScript
- 作者:银子
- 868 位领导视察
通过定时器或enterFrame来实现对像的改变和时间联系起来以实现动画.
两个公式需要记忆一下:
(1) 计算弧度 radians = angle * Math.PI / 180
(2) 计算度数 angle = radians * 180 / Math.PI
一个拖动对象的例子..重点学习的是: startDrag, stopDrag, dropTarget, setChildIndex 等方法的使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | package { import flash.display.Sprite; import flash.display.DisplayObject; import flash.events.MouseEvent; import flash.geom.Point; import flash.filters.DropShadowFilter; public class ClassDrag extends Sprite { private var _red:Sprite; private var _blue:Sprite; private var _green:Sprite; private var _white:Sprite; // 被拖动对象原始坐标 private var _startingLocation:Point; // 构造函数 public function ClassDrag() { init(); } // 初始化 private function init():void { _red = new Sprite(); createRectangle(_red, 0xff0000, 10, 10, 20, 20); _blue = new Sprite(); createRectangle(_blue, 0x0000ff, 10, 35, 20, 20); _green = new Sprite(); createRectangle(_green, 0x00cc00, 10, 60, 20, 20); _white = new Sprite(); createRectangle(_white, 0xf0f0f0, 40, 10, 300, 300, false); } // 创建矩型 private function createRectangle(rectangle:Sprite, color:uint, x:uint, y:uint, width:uint, height:uint, drag:Boolean = true):void { rectangle.graphics.beginFill(color); rectangle.graphics.drawRect(x, y, width, height); rectangle.graphics.endFill(); addChild(rectangle); if(drag) { // 侦听事件 rectangle.addEventListener(MouseEvent.MOUSE_DOWN, pickup); rectangle.addEventListener(MouseEvent.MOUSE_UP, place); } } // 拖动 private function pickup(event:MouseEvent):void { // 保存原始坐标 _startingLocation = new Point(); _startingLocation.x = event.target.x; _startingLocation.y = event.target.y; // 开始拖动 event.target.startDrag(); // 加上阴影 event.target.filters = [new DropShadowFilter()]; // 拖动对象最前显示 setChildIndex(DisplayObject(event.target), numChildren - 1); } // 拖动结束 private function place(event:MouseEvent):void { // 拖动结束 event.target.stopDrag(); // 清除阴影 event.target.filters = null; // 检测是否已经移动到目标区域 if(event.target.dropTarget == _white) { var color:uint; switch(event.target) { case _red: color = 0xff0000; break; case _blue: color = 0x0000ff; break; case _green: color = 0x00cc00; break; } // 重新创建目标区域 _white.graphics.clear(); createRectangle(_white, color, 40, 10, 300, 300, false); } // 拖放对象回到原位 event.target.x = _startingLocation.x; event.target.y = _startingLocation.y; } } } |
很简单的小例子..主要需要掌握的是绘制图形的 drawRect, lineStyle, moveTo, lineTo等方法..鼠标事件侦听的重要性就不再复述.
p.s. cookbook的确是很好的入门书籍.全是实例.^_^
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | package { import flash.display.Sprite; import flash.events.MouseEvent; public class ClassGraphics extends Sprite { private var mySprite:Sprite; // 构造函数 public function ClassGraphics() { this.mySprite = new Sprite(); addChild(this.mySprite); this.mySprite.graphics.beginFill(0x336699); this.mySprite.graphics.drawRect(0, 0, 550, 400); this.mySprite.graphics.endFill(); this.mySprite.addEventListener(MouseEvent.MOUSE_DOWN, spriteMouseDown); this.mySprite.addEventListener(MouseEvent.MOUSE_UP, spriteMouseUp); } // 鼠标按下事件 private function spriteMouseDown(event:MouseEvent):void { this.mySprite.graphics.lineStyle(1, 0, 1); this.mySprite.graphics.moveTo(mouseX, mouseY); this.mySprite.addEventListener(MouseEvent.MOUSE_MOVE, spriteMouseMove); } // 鼠标离开事件 private function spriteMouseUp(event:MouseEvent):void { this.mySprite.removeEventListener(MouseEvent.MOUSE_MOVE, spriteMouseMove); } // 移动事件 private function spriteMouseMove(event:MouseEvent):void { this.mySprite.graphics.lineTo(mouseX, mouseY); } } } |
打算写一个AS3+PHP+MySQL的留言本.(当初学习PHP第一个练习也是留言本-_,-+), 完成部分..使用了Flash CS3 的内置组件: TextInput, TextArea, Label 及 Button
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | package com { import flash.display.Sprite; import flash.events.MouseEvent; import flash.display.StageScaleMode; import flash.display.StageAlign; import fl.controls.Label; import fl.controls.TextInput; import fl.controls.TextArea; import fl.controls.Button; public class ClassMain extends Sprite { private var userName:TextInput; private var userNameLabel:Label; private var message:TextArea; private var messageLabel:Label; private var button:Button; // 构造函数 public function ClassMain() { stage.scaleMode = "noScale"; stage.align = StageAlign.TOP_LEFT; this.initMc(); } // 一点准备工作 private function initMc():void { this.userName = new TextInput(); addInput(this.userName, "userName", 50, 10, 200, 18); this.userNameLabel = new Label(); addLabel(this.userNameLabel, "呢称:", 10, 10, 40, 18); this.message = new TextArea(); addTextArea(this.message, "message", 50, 40, 400, 100); this.messageLabel = new Label(); addLabel(this.messageLabel, "留言:", 10, 40, 40, 18); this.button = new Button(); addButton(this.button, "提交", 350, 150); } // 创建Label private function addLabel(label:Label, text:String, x:uint, y:uint, width:uint, height:uint):void { label.text = text; label.move(x, y); label.setSize(width, height); addChild(label); } // 创建文本框 private function addInput(input:TextInput, name:String, x:uint, y:uint, width:uint, height:uint):void { input.name = name; input.move(x, y); input.setSize(width, height); addChild(input); } // 创建文本域 private function addTextArea(textArea:TextArea, name:String, x:uint, y:uint, width:uint, height:uint):void { textArea.name = name; textArea.move(x, y); textArea.setSize(width, height); textArea.condenseWhite = true; addChild(textArea); } // 创建按钮 private function addButton(button:Button, value:String, x:uint, y:uint):void { button.label= value; button.move(x, y); addChild(button); } } } |
未完成之前..源码还是不放出了..
AS3对XML的操作实在是太方便了(E4X ECMAScript for XML 没错.就是ECMAScript 如果JS比较好的同学对这个也很熟悉吧)..
AS2时读取XML需要用到 firstchild 等奇怪的东东.. 看来没学过AS2也是很有好处的..活活..
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | package { import flash.display.*; import flash.text.*; import flash.xml.*; import flash.net.*; import flash.events.Event; public class ClassSubFeed extends Sprite { var temp:TextField; var myXmlUrl:URLRequest; var myLoader:URLLoader; var xmlUrl:String; var xmlContent:XML; public function ClassSubFeed() { temp = new TextField(); temp.x = 10; temp.y = 10; temp.width = 530; temp.height = 350; temp.border = true; temp.borderColor = 0x669900; temp.multiline = true; temp.htmlText = 'loading.....'; addChild(temp); // xml地址 xmlUrl = "http://www.zdyi.com/feed"; this.loadXml(xmlUrl); } private function loadXml(xmlUrl:String):void { myXmlUrl = new URLRequest(xmlUrl); myLoader = new URLLoader(myXmlUrl); myLoader.addEventListener(Event.COMPLETE, onloaded); } private function onloaded(event):void { xmlContent = XML(myLoader.data); temp.htmlText = '已经读取feed,正在准备.....'; this.parseData(xmlContent); } private function parseData(xmlContent:XML):void { var rrsTitle:String = xmlContent.channel.title; var rrsLink:String = xmlContent.channel.link; temp.htmlText = "<h3><a href='" + rrsLink + "' target='_blank'>" + rrsTitle + "</a></h3><br /><br />"; for each(var item:XML in xmlContent..item) { var itemTitle:String = item.title.toString(); var itemLink:String = item.link.toString(); temp.htmlText += "<div><h4><a href='" + itemLink + "' target='_blank'>" + itemTitle + "</a></h4></div><br /><br />"; } } } } |
最近开始学习AS. 在日志上胡乱记录几个实例..以备回顾..
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | package { import flash.display.MovieClip; import flash.text.TextField; import flash.text.TextFormat; import flash.text.TextFieldAutoSize; import flash.events.*; import flash.utils.Timer; public class ClassTyped extends MovieClip { var txt:TextField = new TextField(); var txtF:TextFormat = new TextFormat(); var cont:String = new String(); var i:uint = 0; // 构造函数 public function ClassTyped() { cont = "打字效果测试. By Silver..........."; txtF.font = "微软雅黑"; txtF.size = 14; txtF.color = 0x669900; txt.defaultTextFormat = txtF; var timer:Timer = new Timer(120, cont.length); timer.addEventListener(TimerEvent.TIMER, timerHandler); timer.start(); } private function timerHandler(event):void { txt.selectable = false; txt.appendText(cont.charAt(i)); txt.autoSize = TextFieldAutoSize.LEFT; txt.wordWrap = true; txt.width = 200; addChild(txt); txt.x = (txt.stage.stageWidth - txt.width) / 2; txt.y = (txt.stage.stageHeight - txt.height) / 2; i++; } } } |