zoukankan      html  css  js  c++  java
  • CocosCreator 根据碰撞法线计算是否在空中

    const{ccclass,property}=cc._decorator;
    
    @ccclass
    export default class Player extends cc.Component{
    	
    	private _rigidBody:cc.RigidBody;
    	private _onGroundB2Contacts:any[]=[];
    	private _isInAir:boolean=true;
    	
    	protected onLoad():void{
    		this._rigidBody=this.node.getComponent(cc.RigidBody);
    		this._rigidBody.enabledContactListener=true;//激活接触侦听
    	}
    	
    	private onPreSolve(contact:cc.PhysicsContact,selfCollider:cc.PhysicsCollider,otherCollider:cc.PhysicsCollider):void{
    		let b2Contact=contact["_b2contact"];
    		
    		//所有设置contact.disabled的代码写在此处
    		
    		//检测落地(不在onBeginContact中写,因为设置contact.disabled时会导致出错)
    		let index=this._onGroundB2Contacts.indexOf(b2Contact);
    		if(index<0){
    			if(!contact.disabled&&!contact.disabledOnce&&!otherCollider.sensor&&contact.isTouching()){
    				let ny=-contact.getWorldManifold().normal.y;
    				if(ny>0.7){
    					this._onGroundB2Contacts.push(b2Contact);
    					if(this._isInAir){
    						this._isInAir=false;
    						//落地时
    						this.onDropGround();
    					}
    				}
    			}
    		}
    	}
    	
    	private onEndContact(contact:cc.PhysicsContact,selfCollider:cc.PhysicsCollider,otherCollider:cc.PhysicsCollider):void{
    		let b2Contact=contact["_b2contact"];
    		let index=this._onGroundB2Contacts.indexOf(b2Contact);
    		if(index>-1)this._onGroundB2Contacts.splice(index,1);
    		if(this._onGroundB2Contacts.length<=0){
    			if(!this._isInAir){
    				this._isInAir=true;
    				//离开地面时
    				this.onLeaveGround();
    			}
    		}
    
    	}
    	
    	private onDropGround():void{
    	
    	}
    	
    	private onLeaveGround():void{
    	
    	}
    }
    
  • 相关阅读:
    java面试笔记6
    Java面试笔记5
    Java面试笔记5
    网上订单管理-新增
    网上订单管理-级联;可编辑列表
    网上订单管理-新增、修改
    数据库嵌套查询
    网上订单管理-数据呈现
    video 移动端内联播放,视频上层可显示弹幕、点赞。video转canvas播放
    vue 日期格式化过滤器
  • 原文地址:https://www.cnblogs.com/kingBook/p/13529128.html
Copyright © 2011-2022 走看看