矩形在as3视觉编程中的几个应用方式
生活随笔
收集整理的這篇文章主要介紹了
矩形在as3视觉编程中的几个应用方式
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
矩形在視覺編程中應用甚廣,下面描述其中的幾種在as3中的應用方式。
本文所用的速度用矢量表示,都是Vector_2D類的實例。
這個2D的矢量類見 http://www.cnblogs.com/vilyLei/articles/1567703.html
1.一個物體被限制在矩形框內運動,碰到邊界就反彈.此處用矩形物體來做示例,此示例代碼直接復制即可用作測試。
as3代碼如下:
代碼 package
{
????/**
??????*?Copyright?(C)?2008-2012?Vily
?????*
?????*?@class?name(類名):????Main
?????*
?????*?@author(創建人):?Vily
?????*
?????*?@version(版本):?v1.1
?????*
?????*?@create?date(創建日期):?2009-12-13
?????*
?????*?@purpose(對此類實現的功能的描述):Main文檔類
?????*
?????*?@public?properties(公開的屬性):?None.?Static?constants?only.
?????*?@public?methods(公開的方法):
?????*?????????????Main(?)?-?Constructor.?
?????*
?????*/
????import?flash.display.Sprite;
????import?flash.events.Event;
????import?flash.geom.Rectangle;
????
????public?class?Main?extends?Sprite{
????????//作為矩形區域的矩形塊
????????private?var?_areaRectSpr:RectSpr?=?null;
????????//作為運動的矩形塊
????????private?var?_currSpr:RectSpr?=?null;
????????//記錄物體運動當前速度的矢量
????????private?var?_currV:Vector_2D?=?new?Vector_2D();
????????private?var?_areaRect:Rectangle?=?null;
????????public?function?Main(){
????????????init();
????????}
????????/**
?????????*????系統程序初始化入口
?????????*/
????????private?function?init():void{
????????????
????????????initDispObj();
????????????initListener();
????????}????????
????????/**
?????????*????初始化顯示對象
?????????*/
????????private?function?initDispObj():void{
????????????
????????????//矩形區域
????????????_areaRect?=?new?Rectangle(50,50,450,300);
????????????_areaRectSpr?=?new?RectSpr();
????????????addChild(_areaRectSpr);
????????????_areaRectSpr.x?=?_areaRect.x;
????????????_areaRectSpr.y?=?_areaRect.y;
????????????_areaRectSpr.drawBg(_areaRect.width,_areaRect.height);
????????????
????????????//運動的矩形可是對象
????????????_currSpr?=?new?RectSpr();
????????????_currSpr.drawBg(30,30,0xff0000);
????????????_currSpr.x?=?200;
????????????_currSpr.y?=?200;
????????????addChild(_currSpr);
????????????
????????????_currV.x?=?Math.round(Math.random()?*?4);
????????????_currV.y?=?Math.round(Math.random()?*?4);
????????}
????????/**
?????????*????初始化偵聽器
?????????*/
????????private?function?initListener():void{
????????????this.addEventListener(Event.ENTER_FRAME,enterTest);
????????}
????????private?function?enterTest(evt:Event):void{
????????????
????????????//檢測運動的_currSpr是否碰到矩形的左右邊界
????????????var?px:Number?=?0;
????????????if(_currV.x?>?0){
????????????????px?=?_currSpr.x?+?_currV.x;
????????????????if(px?>?(_areaRect.x?+?_areaRect.width?-?_currSpr.width)){
????????????????????px?=?_areaRect.x?+?_areaRect.width?-?_currSpr.width;
????????????????????_currV.x?=?-_currV.x;
????????????????}
????????????}else{
????????????????px?=?_currSpr.x?+?_currV.x;
????????????????if(px?<?_areaRect.x){
????????????????????px?=?_areaRect.x;
????????????????????_currV.x?=?-_currV.x;
????????????????}
????????????}
????????????
????????????//檢測運動的_currSpr是否碰到矩形的上下邊界
????????????var?py:Number?=?0;
????????????if(_currV.y?>?0){
????????????????py?=?_currSpr.y?+?_currV.y;
????????????????if(py?>?(_areaRect.y?+?_areaRect.height?-?_currSpr.height)){
????????????????????py?=?_areaRect.y?+?_areaRect.height?-?_currSpr.height;
????????????????????_currV.y?=?-_currV.y;
????????????????}
????????????}else{
????????????????py?=?_currSpr.y?+?_currV.y;
????????????????if(py?<?_areaRect.y){
????????????????????py?=?_areaRect.y;
????????????????????_currV.y?=?-_currV.y;
????????????????}
????????????}
????????????_currSpr.x?=?px;
????????????_currSpr.y?=?py;
????????}
????}
}
import?flash.display.Sprite;
class?RectSpr?extends?Sprite{
????public?function?RectSpr(){}
????public?function?drawBg(w:Number,h:Number,color:uint?=?0x0):void{
????????this.graphics.beginFill(color);
????????this.graphics.drawRect(0,0,w,h);
????????this.graphics.endFill();
????}
}
?
2.檢測是否碰撞到矩形,如果碰到障礙物就反彈(A)
本示例?以運動的矩形物體的中心點為檢測條件。也就相當于判定一個點是否和一個矩形相碰撞。
而碰撞后反彈時的速度矢量則使用逆向算法計算的到.
as3代碼如下:
代碼 package{
????/**
??????*?Copyright?(C)?2008-2012?Vily
?????*
?????*?@class?name(類名):????Main
?????*
?????*?@author(創建人):?Vily
?????*
?????*?@version(版本):?v1.1
?????*
?????*?@create?date(創建日期):?2009-12-13
?????*
?????*?@purpose(對此類實現的功能的描述):Main文檔類
?????*
?????*?@public?properties(公開的屬性):?None.?Static?constants?only.
?????*?@public?methods(公開的方法):
?????*?????????????Main(?)?-?Constructor.?
?????*
?????*/
????import?flash.display.Sprite;
????import?flash.events.Event;
????import?flash.geom.Rectangle;
????
????public?class?Main?extends?Sprite{
????????//作為矩形區域的矩形塊
????????private?var?_areaRectSpr:RectSpr?=?null;
????????//作為運動的矩形塊
????????private?var?_currSpr:RectSpr?=?null;
????????//記錄物體運動當前速度的矢量
????????private?var?_currV:Vector_2D?=?new?Vector_2D();
????????//限制小矩形塊運動的矩形區域
????????private?var?_areaRect:Rectangle?=?null;????????
????????
????????//臨時矢量
????????private?var?_tempV:Vector_2D?=?new?Vector_2D();
????????//矩形障礙物所占的矩形
????????private?var?_blockRect:Rectangle?=?null;
????????//作為矩形障礙物的RectSpr實例
????????private?var?_blockSpr:RectSpr?=?null;
????????public?function?Main(){
????????????init();
????????}
????????/**
?????????*????系統程序初始化入口
?????????*/
????????private?function?init():void{
????????????
????????????initDispObj();
????????????initListener();
????????}????????
????????/**
?????????*????初始化顯示對象
?????????*/
????????private?function?initDispObj():void{
????????????
????????????//矩形區域
????????????_areaRect?=?new?Rectangle(50,50,450,300);
????????????_areaRectSpr?=?new?RectSpr();
????????????addChild(_areaRectSpr);
????????????_areaRectSpr.x?=?_areaRect.x;
????????????_areaRectSpr.y?=?_areaRect.y;
????????????_areaRectSpr.drawBg(_areaRect.width,_areaRect.height);
????????????
????????????
????????????//矩形障礙物
????????????_blockRect?=?new?Rectangle(240,120,80,150);
????????????_blockSpr?=?new?RectSpr();
????????????addChild(_blockSpr);
????????????_blockSpr.x?=?_blockRect.x;
????????????_blockSpr.y?=?_blockRect.y;
????????????_blockSpr.drawBg(_blockRect.width,_blockRect.height,0x008800);
????????????
????????????//運動的矩形可是對象
????????????_currSpr?=?new?RectSpr();
????????????_currSpr.drawBg(30,30,0xff0000);
????????????_currSpr.x?=?81;
????????????_currSpr.y?=?81;
????????????addChild(_currSpr);
????????????
????????????_currV.x?=?Math.round(Math.random()?*?4);
????????????_currV.y?=?Math.round(Math.random()?*?4);
????????}
????????/**
?????????*????初始化偵聽器
?????????*/
????????private?function?initListener():void{
????????????this.addEventListener(Event.ENTER_FRAME,enterTest);
????????}
????????//計算碰到矩形邊界后的速度矢量
????????protected?function?calcHitRectBoundsV(posV:Vector_2D,speed_v:Vector_2D,rect:Rectangle):Boolean{
????????????var?px:Number?=?posV.x?+?speed_v.x;
????????????if(px?<=?rect.x?||?px?>=?(rect.x?+?rect.width)){
????????????????//碰到左邊界
????????????????speed_v.x?=?-speed_v.x;
????????????????return?true;
????????????}
????????????var?py:Number?=?posV.y?+?speed_v.y;
????????????if(py?<=?rect.y?||?py?>=?(rect.y?+?rect.height)){
????????????????????//碰到左邊界
????????????????????speed_v.y?=?-speed_v.y;
????????????????????return?true;????????????????
????????????}
????????????return?false;
????????}
????????private?function?enterTest(evt:Event):void{
????????????
????????????var?py:Number?=?0;
????????????var?px:Number?=?0;
????????????//檢測是否碰到障礙物
????????????//計算_currSpr下一步運動后的中心點坐標
????????????_tempV.x?=?_currSpr.x?+?_currSpr.width/2?+?_currV.x;
????????????_tempV.y?=?_currSpr.y?+?_currSpr.height/2?+?_currV.y;
????????????//先判斷小矩形塊的中心點是否和矩形障礙物相碰
????????????if(_blockRect.contains(_tempV.x,_tempV.y)){
????????????????
????????????????//這里采用逆向算法來計算碰撞后的速度矢量
????????????????_currV.x?=?-_currV.x;
????????????????_currV.y?=?-_currV.y;
????????????????var?boo:Boolean?=?calcHitRectBoundsV(_tempV,_currV,_blockRect);
????????????????_currV.x?=?-_currV.x;
????????????????_currV.y?=?-_currV.y;
????????????????
????????????????//如果已經碰撞,那就要反彈.
????????????????if(boo){
????????????????????return;
????????????????}
????????????}
????????????
????????????//檢測運動的_currSpr是否碰到矩形的左右邊界????????????
????????????if(_currV.x?>?0){
????????????????px?=?_currSpr.x?+?_currV.x;
????????????????if(px?>?(_areaRect.x?+?_areaRect.width?-?_currSpr.width)){
????????????????????px?=?_areaRect.x?+?_areaRect.width?-?_currSpr.width;
????????????????????_currV.x?=?-_currV.x;
????????????????}
????????????}else{
????????????????px?=?_currSpr.x?+?_currV.x;
????????????????if(px?<?_areaRect.x){
????????????????????px?=?_areaRect.x;
????????????????????_currV.x?=?-_currV.x;
????????????????}
????????????}
????????????
????????????//檢測運動的_currSpr是否碰到矩形的上下邊界????????????
????????????if(_currV.y?>?0){
????????????????py?=?_currSpr.y?+?_currV.y;
????????????????if(py?>?(_areaRect.y?+?_areaRect.height?-?_currSpr.height)){
????????????????????py?=?_areaRect.y?+?_areaRect.height?-?_currSpr.height;
????????????????????_currV.y?=?-_currV.y;
????????????????}
????????????}else{
????????????????py?=?_currSpr.y?+?_currV.y;
????????????????if(py?<?_areaRect.y){
????????????????????py?=?_areaRect.y;
????????????????????_currV.y?=?-_currV.y;
????????????????}
????????????}????????????
????????????_currSpr.x?=?px;
????????????_currSpr.y?=?py;
????????}
????}
????
????
}
import?flash.display.Sprite;
class?RectSpr?extends?Sprite{
????public?function?RectSpr(){}
????public?function?drawBg(w:Number,h:Number,color:uint?=?0x0):void{
????????this.graphics.beginFill(color);
????????this.graphics.drawRect(0,0,w,h);
????????this.graphics.endFill();
????}
}
?
?
3.檢測是否碰撞到矩形,如果碰到障礙物就反彈(B)
本示例?以運動的矩形物體所占據的矩形區域為檢測條件。也就相當于判定兩個矩形相的碰撞。
而碰撞后反彈時的速度矢量則使用矩形重疊算法計算的到.
?
代碼 package{
????/**
??????*?Copyright?(C)?2008-2012?Vily
?????*
?????*?@class?name(類名):????Main
?????*
?????*?@author(創建人):?Vily
?????*
?????*?@version(版本):?v1.1
?????*
?????*?@create?date(創建日期):?2009-12-13
?????*
?????*?@purpose(對此類實現的功能的描述):Main文檔類
?????*
?????*?@public?properties(公開的屬性):?None.?Static?constants?only.
?????*?@public?methods(公開的方法):
?????*?????????????Main(?)?-?Constructor.?
?????*
?????*/
????import?flash.display.Sprite;
????import?flash.events.Event;
????import?flash.geom.Rectangle;
????
????public?class?Main?extends?Sprite{
????????//作為矩形區域的矩形塊
????????private?var?_areaRectSpr:RectSpr?=?null;
????????//作為運動的矩形塊
????????private?var?_currSpr:RectSpr?=?null;
????????//記錄物體運動當前速度的矢量
????????private?var?_currV:Vector_2D?=?new?Vector_2D();
????????//限制小矩形塊運動的矩形區域
????????private?var?_areaRect:Rectangle?=?null;????????
????????
????????//臨時矢量
????????private?var?_tempV:Vector_2D?=?new?Vector_2D();
????????//臨時矩形變量
????????private?var?_tempRect:Rectangle?=?new?Rectangle();
????????private?var?_interRect:Rectangle?=?null;
????????
????????//矩形障礙物所占的矩形
????????private?var?_blockRect:Rectangle?=?null;
????????//作為矩形障礙物的RectSpr實例
????????private?var?_blockSpr:RectSpr?=?null;
????????public?function?Main(){
????????????init();
????????}
????????/**
?????????*????系統程序初始化入口
?????????*/
????????private?function?init():void{
????????????
????????????initDispObj();
????????????initListener();
????????}????????
????????/**
?????????*????初始化顯示對象
?????????*/
????????private?function?initDispObj():void{
????????????
????????????//矩形區域
????????????_areaRect?=?new?Rectangle(50,50,450,300);
????????????_areaRectSpr?=?new?RectSpr();
????????????addChild(_areaRectSpr);
????????????_areaRectSpr.x?=?_areaRect.x;
????????????_areaRectSpr.y?=?_areaRect.y;
????????????_areaRectSpr.drawBg(_areaRect.width,_areaRect.height);
????????????
????????????
????????????//矩形障礙物
????????????_blockRect?=?new?Rectangle(240,120,80,150);
????????????_blockSpr?=?new?RectSpr();
????????????addChild(_blockSpr);
????????????_blockSpr.x?=?_blockRect.x;
????????????_blockSpr.y?=?_blockRect.y;
????????????_blockSpr.drawBg(_blockRect.width,_blockRect.height,0x008800);
????????????
????????????//運動的矩形可是對象
????????????_currSpr?=?new?RectSpr();
????????????_currSpr.drawBg(30,30,0xff0000);
????????????_currSpr.x?=?81;
????????????_currSpr.y?=?81;
????????????addChild(_currSpr);
????????????
????????????_currV.x?=?Math.round(Math.random()?*?4);
????????????_currV.y?=?Math.round(Math.random()?*?4);
????????????_tempRect.width?=?_currSpr.width;
????????????_tempRect.height?=?_currSpr.height;
????????}
????????/**
?????????*????初始化偵聽器
?????????*/
????????private?function?initListener():void{
????????????this.addEventListener(Event.ENTER_FRAME,enterTest);
????????}????????
????????private?function?enterTest(evt:Event):void{
????????????
????????????var?py:Number?=?0;
????????????var?px:Number?=?0;
????????????
????????????//計算_currSpr下一步運動后的矩形數據
????????????_tempRect.x?=?_currSpr.x?+?_currV.x;
????????????_tempRect.y?=?_currSpr.y?+?_currV.y;
????????????//判斷小矩形塊是否和矩形障礙物相碰,如果相碰就反彈
????????????_interRect?=?_tempRect.intersection(_blockRect);
????????????if(_interRect.width>0?||?_interRect.height>0?){
????????????????if(_interRect.width?<?_interRect.height){
????????????????????????_currV.x?=?-_currV.x;
????????????????}else{
????????????????????_currV.y?=?-_currV.y;
????????????????}
????????????????return;
????????????}
????????????
????????????//檢測運動的_currSpr是否碰到矩形的左右邊界
????????????if(_currV.x?>?0){
????????????????px?=?_currSpr.x?+?_currV.x;
????????????????if(px?>?(_areaRect.x?+?_areaRect.width?-?_currSpr.width)){
????????????????????px?=?_areaRect.x?+?_areaRect.width?-?_currSpr.width;
????????????????????_currV.x?=?-_currV.x;
????????????????}
????????????}else{
????????????????px?=?_currSpr.x?+?_currV.x;
????????????????if(px?<?_areaRect.x){
????????????????????px?=?_areaRect.x;
????????????????????_currV.x?=?-_currV.x;
????????????????}
????????????}
????????????
????????????//檢測運動的_currSpr是否碰到矩形的上下邊界????????????
????????????if(_currV.y?>?0){
????????????????py?=?_currSpr.y?+?_currV.y;
????????????????if(py?>?(_areaRect.y?+?_areaRect.height?-?_currSpr.height)){
????????????????????py?=?_areaRect.y?+?_areaRect.height?-?_currSpr.height;
????????????????????_currV.y?=?-_currV.y;
????????????????}
????????????}else{
????????????????py?=?_currSpr.y?+?_currV.y;
????????????????if(py?<?_areaRect.y){
????????????????????py?=?_areaRect.y;
????????????????????_currV.y?=?-_currV.y;
????????????????}
????????????}????????????
????????????_currSpr.x?=?px;
????????????_currSpr.y?=?py;
????????}
????}
????
????
}
import?flash.display.Sprite;
class?RectSpr?extends?Sprite{
????public?function?RectSpr(){}
????public?function?drawBg(w:Number,h:Number,color:uint?=?0x0):void{
????????this.graphics.beginFill(color);
????????this.graphics.drawRect(0,0,w,h);
????????this.graphics.endFill();
????}
}
??
注意,在實際的應用中可能需要第二點和第三點結合使用。否則可能有偏差。
以上描述如果有問題,請您留言提示我,在此先謝謝了。
?
轉載于:https://www.cnblogs.com/vilyLei/articles/1623536.html
總結
以上是生活随笔為你收集整理的矩形在as3视觉编程中的几个应用方式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 写了几天的软工课程设计,慢慢了解了点mv
- 下一篇: 此心拖泥带水,是人生最苦处