as3.0常用代码
1、检测碰撞 check.addEventListener(Event.ENTER_FRAME, checkCollision); function checkCollision(event:Event) { //hitTestPoint检测对象与某点是否发生碰撞 if (crescent.hitTestPoint(mouseX, mouseY, true)) { messageText1.text = "hitTestPoint: YES"; } else { messageText1.text = "hitTestPoint: NO"; } // move star with mouse star.x = mouseX; star.y = mouseY; //hitTestObject检测两物体是否碰撞 if (star.hitTestObject(crescent)) { messageText2.text = "hitTestObject: YES"; } else { messageText2.text = "hitTestObject: NO"; } } 2、使用文本框 //创建基本格式 var myFormat:TextFormat = new TextFormat(); myFormat.font = "Arial"; myFormat.size = 24; myFormat.bold = true; //创建样式表 var myStyleSheet:StyleSheet = new StyleSheet(); myStyleSheet.setStyle("A",{textDecoration: "underline", color: "#0000FF"}); var myLink:TextField = new TextField(); //使用格式化文本 myLink.defaultTextFormat = myFormat; //文本使用样式表 myLink.styleSheet = myStyleSheet; myLink.selectable = false; myLink.htmlText = "Click <A HREF='event:testing'>here</A>"; myLink.x = 80; myLink.y = 190; myLink.width = 400; myLink.height = 30; addChild(myLink); //添加文本单击事件 addEventListener(TextEvent.LINK, textLinkClick); function textLinkClick(event:TextEvent) { trace(event.text); } 3、使用自定义鼠标 Mouse.hide(); addEventListener(Event.ENTER_FRAME, moveCursor); arrow.mouseEnabled = false; function moveCursor(event:Event) { arrow.x = mouseX; arrow.y = mouseY; } 4、简单计数器 var timeDisplay:TextField = new TextField(); addChild(timeDisplay); var startTime:int = getTimer(); addEventListener(Event.ENTER_FRAME, showClock); function showClock(event:Event) { var timePassed:int = getTimer()-startTime; var seconds:int = Math.floor(timePassed/1000); var minutes:int = Math.floor(seconds/60); seconds -= minutes*60; var timeString:String = minutes+":"+String(seconds+100).substr(1,2); timeDisplay.text = timeString; } 5、拖动影片 mascot.addEventListener(MouseEvent.MOUSE_DOWN, startMascotDrag); stage.addEventListener(MouseEvent.MOUSE_UP, stopMascotDrag); mascot.addEventListener(Event.ENTER_FRAME, dragMascot); var clickOffset:Point = null; function startMascotDrag(event:MouseEvent) { clickOffset = new Point(event.localX, event.localY); } function stopMascotDrag(event:MouseEvent) { clickOffset = null; } function dragMascot(event:Event) { if (clickOffset != null) { // must be dragging mascot.x = mouseX - clickOffset.x; mascot.y = mouseY - clickOffset.y; } } 6、使用动态绘图函数 // 画(100,200)到(150,250)的直线 this.graphics.lineStyle(2,0x000000); this.graphics.moveTo(100,200); this.graphics.lineTo(150,250); // 画曲线 this.graphics.curveTo(200,300,250,250); // 画矩形、圆角矩形 this.graphics.drawRect(50,50,300,250); this.graphics.drawRoundRect(40,40,320,270,25,25); // 画圆、椭圆 this.graphics.drawCircle(150,100,20); this.graphics.drawEllipse(180,150,40,70); // 填充圆 this.graphics.beginFill(0x333333); this.graphics.drawCircle(250,100,20); this.graphics.endFill(); 7、捕捉键盘输入 // 显示按下的键 var keyboardText:TextField = new TextField(); keyboardText.selectable = false; addChild(keyboardText); //用于显示spacbar是否按下 var spacebarText:TextField = new TextField(); spacebarText.selectable = false; spacebarText.y = 30; addChild(spacebarText); spacebarText.text = "Spacebar is UP."; var spacebarPressed:Boolean = false; stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownFunction); function keyDownFunction(event:KeyboardEvent) { keyboardText.text = "Key Pressed: "+String.fromCharCode(event.charCode); if (event.charCode == 32) { spacebarPressed = true; spacebarText.text = "Spacebar is DOWN."; } } stage.addEventListener(KeyboardEvent.KEY_UP, keyUpFunction); function keyUpFunction(event:KeyboardEvent) { if (event.charCode == 32) { spacebarPressed = false; spacebarText.text = "Spacebar is UP."; } } 8、加载HTML页面变量(difficultyLevel变量由HTML'flashvars'设置) var paramObj:Object = LoaderInfo(this.root.loaderInfo).parameters; var diffLevel:String = paramObj["difficultyLevel"]; var messageString = "difficultyLevel: "+paramObj["difficultyLevel"]; messageString += "\n"; messageString += "puzzleFile: "+paramObj["puzzleFile"]; messageText.text = messageString; 9、加载外部XML格式文本 var xmlURL:URLRequest = new URLRequest("LoadingData.xml"); var xmlLoader:URLLoader = new URLLoader(xmlURL); xmlLoader.addEventListener(Event.COMPLETE, xmlLoaded); function xmlLoaded(event:Event) { var dataXML = XML(event.target.data); trace(dataXML.question.text); trace(dataXML.question.answers.answer[0]); trace(dataXML.question.answers.answer[0].@type); } 10、建立预加载动画 stop(); addEventListener(Event.ENTER_FRAME, loadProgress); function loadProgress(event:Event) { // 获得总字节数和已经加载的字节数 var movieBytesLoaded:int = this.root.loaderInfo.bytesLoaded; var movieBytesTotal:int = this.root.loaderInfo.bytesTotal; // 转化为MB var movieKLoaded:int = movieBytesLoaded/1024; var movieKTotal:int = movieBytesTotal/1024; progressText.text = "Loading: "+movieKLoaded+"K/"+movieKTotal+"K"; if (movieBytesLoaded >= movieBytesTotal) { removeEventListener(Event.ENTER_FRAME, loadProgress); gotoAndStop(2); } } 11、动态建立按钮元件 函数:SimpleButton(upState:DisplayObject = null, overState:DisplayObject = null, downState:DisplayObject = null, hitTestState:DisplayObject = null) var mySimpleButton:SimpleButton = new SimpleButton(new ButtonUp(), new ButtonOver(), new ButtonDown(), new ButtonHit()); mySimpleButton.x = 450; mySimpleButton.y = 250; addChild(mySimpleButton); mySimpleButton.addEventListener(MouseEvent.CLICK, clickSimpleButton); function clickSimpleButton(event:MouseEvent) { trace("You clicked the simple button!"); } 12、使用声音 button1.addEventListener(MouseEvent.CLICK, playLibrarySound); button2.addEventListener(MouseEvent.CLICK, playExternalSound); // 加载外部声音 var sound2:Sound = new Sound(); var req:URLRequest = new URLRequest("PlayingSounds.mp3"); sound2.load(req); function playLibrarySound(event:Event) { var sound1:Sound1 = new Sound1(); var channel:SoundChannel = sound1.play(); } function playExternalSound(event:Event) { sound2.play(); } 13、生成随机数 var random1:Number = Math.random(); trace("Random Number Between 0-0.9999: "+random1); var random2:Number = Math.random()*100; trace("Random Number Between 0-99.9999: "+random2); var random3:Number = Math.floor(Math.random()*100); trace("Random Integer Between 0-99: "+random3); var random4:Number = Math.floor(Math.random()*100)+1; trace("Random Integer Between 1-100: "+random4); 14、存储数据 var myLocalData:SharedObject = SharedObject.getLocal("mygamedata"); trace("Found Data: "+myLocalData.data.gameinfo); myLocalData.data.gameinfo = "Store this."; 15、随机排列数组元素 // 创建有序的数组 var startDeck:Array = new Array(); for(var cardNum:int=0;cardNum<52;cardNum++) { startDeck.push(cardNum); } trace("Unshuffled:",startDeck); // 打乱数据 var shuffledDeck:Array = new Array(); while (startDeck.length > 0) { var r:int = Math.floor(Math.random()*startDeck.length); shuffledDeck.push(startDeck[r]); startDeck.splice(r,1); } trace("Shuffled:", shuffledDeck); 16、获取环境信息 function showInfo(event:Event) { var output:String = ""; output += "stageWidth: "+stage.stageWidth+"\n"; output += "stageHeight: "+stage.stageHeight+"\n"; output += "playerType: "+Capabilities.playerType+"\n"; output += "language: "+Capabilities.language+"\n"; output += "os: "+Capabilities.os+"\n"; output += "screenResolutionX: "+Capabilities.screenResolutionX+"\n"; output += "screenResolutionY: "+Capabilities.screenResolutionY+"\n"; output += "version: "+Capabilities.version+"\n"; output += "serverString: "+Capabilities.serverString+"\n"; messageText.text = output; } stage.scaleMode = StageScaleMode.EXACT_FIT; addEventListener(Event.ENTER_FRAME,showInfo); 17、接受文本输入 myInput.addEventListener(KeyboardEvent.KEY_UP, checkForReturn); function checkForReturn(event:KeyboardEvent) { if (event.charCode == 13) { acceptInput(); } } function acceptInput() { var theInputText:String = myInput.text; trace(theInputText); removeChild(myInput); }