📄 ch01.txt
字号:
chapter: ActionScript Basics==================-default-size width height-default-background-color color-default-frame-rate fps ====================================-default-size 800 600-default-background-color 0xffffff-default-frame-rate 31 ====================================-default-size 800 600 -default-frame-rate 31 ====================================[SWF(width="800", height="600", backgroundColor="#ffffff", frameRate="31")] ====================================package { import flash.display.Sprite; public class ExampleApplication extends Sprite { public function ExampleApplication() { } }} ====================================package com.as3cb.utils {} ====================================import com.as3cb.utils.StringUtils; ====================================package { import flash.display.Sprite; public class ExampleApplication extends Sprite { public function ExampleApplication() { graphics.lineStyle(1, 0, 1); for(var i:int=0;i<100;i++) { graphics.lineTo(Math.random() * 400, Math.random() * 400); } } }} ====================================trace("Hello, world");trace(userName);trace("My name is " + userName + "."); ====================================package { import flash.display.Sprite; public class ExampleApplication extends Sprite { public function ExampleApplication() { var userName:String = "Bill Smith"; trace("My name is " + userName + "."); } }} ====================================TraceOutputFileEnable=1 ====================================TraceOutputFileEnable=1TraceOutputFileName=C:\\flex.log ====================================addEventListener(type:String, listener:Function) ====================================addEventListener(Event.ENTER_FRAME, onEnterFrame); ====================================import flash.events.Event;private function onEnterFrame(event:Event) {} ====================================package { import flash.display.Sprite; import flash.events.Event; public class ExampleApplication extends Sprite { public function ExampleApplication() { graphics.lineStyle(1, 0, 1); addEventListener(Event.ENTER_FRAME, onEnterFrame); } private function onEnterFrame(event:Event):void { graphics.lineTo(Math.random() * 400, Math.random() * 400); } }} ====================================package { import flash.display.Sprite; import flash.events.MouseEvent; public class ExampleApplication extends Sprite { private var _sprite:Sprite; public function ExampleApplication() { _sprite = new Sprite(); addChild(_sprite); _sprite.graphics.beginFill(0xffffff); _sprite.graphics.drawRect(0, 0, 400, 400); _sprite.graphics.endFill(); ==================================== _sprite.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown); _sprite.addEventListener(MouseEvent.MOUSE_UP, onMouseUp); } ==================================== private function onMouseDown(event:MouseEvent):void { _sprite.graphics.lineStyle(1, 0, 1); _sprite.graphics.moveTo(mouseX, mouseY); _sprite.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove); } private function onMouseUp(event:MouseEvent):void { _sprite.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove); } ==================================== private function onMouseMove(event:MouseEvent):void { _sprite.graphics.lineTo(mouseX, mouseY); } }} ====================================stage.focus = this; ====================================package { import flash.display.Sprite; import flash.events.KeyboardEvent; public class ExampleApplication extends Sprite { public function ExampleApplication() { stage.focus = this; addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown); } private function onKeyDown(event:KeyboardEvent):void { trace("key down: " + event.charCode); } }} ====================================// Add 6 to the current value of quantity, and assign that new // value back to quantity. For example, if quantity was 4, this // statement sets it to 10.quantity = quantity + 6; ====================================quantity = quantity + 6;quantity += 6; ====================================quantity = quantity - 6;quantity -= 6; ====================================quantity = quantity * factor;quantity *= factor; ====================================quantity = quantity / factor;quantity /= factor; ====================================quantity++; ====================================quantity = quantity + 1;quantity += 1; ====================================quantity --; ====================================quantity = quantity - 1;quantity -= 1; ====================================var quantity:Number = 5;trace(quantity++); // Displays: 5trace(quantity); // Displays: 6var quantity:Number = 5;trace(++quantity); // Displays: 6trace(quantity); // Displays: 6 ====================================private function onEnterFrame(event:Event) { _sprite.rotation += 5;} ====================================trace(5 == 6); // Displays: falsetrace(6 == 6); // Displays: truetrace(6 == "6"); // Displays: truetrace(5 == "6"); // Displays: false ====================================trace(5 != 6); // Displays: truetrace(6 != 6); // Displays: falsetrace(6 != "6"); // Displays: falsetrace(5 != "6"); // Displays: true ====================================trace(6 === 6); // Displays: truetrace(6 === "6"); // Displays: falsetrace(6 !== 6); // Displays: falsetrace(6 !== "6"); // Displays: true ====================================var quantity:int = 5;// The following code is wrong. It should be if (quantity == 6) insteadif (quantity = 6) { trace("Rabbits are bunnies.");}trace("quantity is " + quantity); // Displays: quantity is 6 ====================================var quantity:int = 5;if (quantity is int) { trace("Yippee. It's an integer.");} ====================================var quantity:Number = 15 - "rabbits"; ====================================trace(typeof quantity); // Displays: "number" ====================================var quantity:Number = 15 - "rabbits";if (quantity is Number) { // Nice try, but this won't work if (quantity != NaN) { trace("Yippee. It's a number."); }} ====================================var quantity:Number = 15 - "rabbits";if (isNaN(quantity)) { trace("Sorry, that is not a valid number.");} ====================================var quantity:Number = 15 - "rabbits";if (!isNaN(quantity)) { // The number is not invalid, so it must be a valid number trace ("That is a valid number.");} ====================================trace(5 < 6); // Displays: truetrace(5 > 5); // Displays: false ====================================trace(5 <= 6); // Displays: truetrace(5 >= 5); // Displays: true ====================================var quantity:Number = 6;var total:Number = 6;trace (quantity == total); // Displays: true ====================================// Create two arrays with the same elements.var arrayOne:Array = new Array("a", "b", "c");var arrayTwo:Array = new Array("a", "b", "c");trace(arrayOne == arrayTwo); // Displays: false ====================================// Create a single arrayvar arrayOne:Array = new Array("a", "b", "c");// Create another variable that references the same array.var arrayTwo:Array = arrayOne;trace(arrayOne == arrayTwo); // Displays: true ====================================if (animalName == "turtle") { // This trace() statement executes only when animalName is equal // to "turtle". trace("Yay! 'Turtle' is the correct answer.");} ====================================if (animalName == "turtle") { // These statements execute only when animalName is equal // to "turtle". showMessage("Yay! 'Turtle' is the correct answer.");}else { // These statements execute only when animalName is not equal // to "turtle". showMessage("Sorry, you got the question wrong."); } ====================================if (animalName == "turtle") { // This trace() statement executes only when animalName is equal // to "turtle". showMessage ("Yay! 'Turtle' is the correct answer.");}else if (animalName == "dove") { // This trace() statement executes only when animalName is not // "turtle", but is "dove". showMessage ("Sorry, a dove is a bird, not a reptile.");} ====================================if (animalName == "turtle") { // This trace() statement executes only when animalName is equal // to "turtle". showMessage ("Yay! 'Turtle' is the correct answer.");}else if (animalName == "dove") { // This statement executes only when animalName is not // "turtle", but is "dove". showMessage ("Sorry, a dove is a bird, not a reptile.");}else { // This statement executes only when animalName is neither // "turtle" nor "dove". showMessage ("Sorry, try again.");} ====================================switch (testExpression) { case caseExpression: // case body case caseExpression: // case body default: // case body} ====================================var animalName:String = "dove";/* In the following switch statement, the first trace() statement does not execute because animalName is not equal to "turtle". But both the second and third trace() statements execute, because once the "dove" case tests true, all subsequent code is executed.*/switch (animalName) { case "turtle": trace("Yay! 'Turtle' is the correct answer."); case "dove": trace("Sorry, a dove is a bird, not a reptile."); default: trace("Sorry, try again.");} ====================================var animalName:String = "dove";// Now, only the second trace() statement executes.switch (animalName) { case "turtle": trace("Yay! 'Turtle' is the correct answer."); break; case "dove": trace("Sorry, a dove is a bird, not a reptile."); break; default: trace("Sorry, try again.");} ====================================switch (animalName) { case "turtle": case "alligator": case "iguana": trace("Yay! You named a reptile."); break; case "dove": case "pigeon": case "cardinal": trace("Sorry, you specified a bird, not a reptile."); break; default: trace("Sorry, try again.");} ====================================varName = (conditional expression) ? valueIfTrue : valueIfFalse; ====================================// Check if today is April 17th.var current:Date = new Date();if (current.getDate() == 17 && current.getMonth() == 3) { trace ("Happy Birthday, Bruce!");} ====================================// Check if today is April 17th.if ((current.getDate() == 17) && (current.getMonth() == 3)) { trace ("Happy Birthday, Bruce!");}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -