123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- import { _decorator, Component, Node,Vec2,Vec3 ,SpriteFrame,find,tween,Tween,Collider2D,Contact2DType,IPhysics2DContact, Sprite} from 'cc';
- const { ccclass, property } = _decorator;
- const RADIAN = Math.PI / 180;
- import { Game } from './Game';
- import { Item } from './Item';
- @ccclass('Buttet')
- export class Buttet extends Component {
- @property([SpriteFrame])
- frames:SpriteFrame[]=[];
- rotation:number=-30;
- speed:number=800;
- isUse:boolean;
- targetIndex:number;
- game:Game;
- start() {
- console.log("targetIndex==>>"+this.targetIndex)
- this.game=find("Canvas/GameMainPanel/GameRoot/Game/GameBg/Game").getComponent(Game)
- this.isUse=true
- this.node.getChildByName("Sprite").getComponent(Sprite).spriteFrame=this.frames[this.targetIndex]
- // 注册单个碰撞体的回调函数
- let collider = this.node.getChildByName("Sprite").getComponent(Collider2D);
- if (collider) {
- collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
- }
- this.move()
- }
- onBeginContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
- // 只在两个碰撞体开始接触时被调用一次
- // console.log('onBeginContact==>>'+otherCollider.tag);
- if(!this.isUse) return;
- if(otherCollider.tag==1){
- this.game.playGet()
- this.isUse=false;
- if(otherCollider.node.parent.isValid)
- otherCollider.node.parent.destroy()
- // otherCollider.node.parent.active=false
- if(this.node.isValid)
- this.node.destroy()
- let otherIndex=otherCollider.node.parent.getComponent(Item).index
- console.log('onBeginContact2==>>'+this.node.isValid+","+otherIndex+","+this.targetIndex);
- if(this.targetIndex==otherIndex)
- {
- this.game.getItem(true)
- }else{
- this.game.getItem(false)
- }
- }
- }
- move(){
- //计算终点
- let endPoint = new Vec2();
- endPoint.x = 1000 * Math.sin(this.rotation);// * RADIAN);
- endPoint.y = 1000 * Math.cos(this.rotation);// * RADIAN);
- // console.log("_emmitNode==2>>"+endPoint)
- // let tmpVec= new Vec3(endPoint.x,endPoint.y,0).subtract(this.node.position).normalize();
- // let normalizeVec=new Vec2(tmpVec.x,tmpVec.y);
- // this.node.angle=new Vec2(0, 1).signAngle(normalizeVec) * 180 / Math.PI;
- let distance = new Vec3(endPoint.x,endPoint.y,0).subtract(this.node.position).length();
- let duration = distance / this.speed;
- tween(this.node)
- .to(duration, {
- position: new Vec3(endPoint.x,endPoint.y,0),
- }, {
- onComplete: (target?: object) => { // 回调,当缓动动作更新时触发。
- this.node.destroy()
- },
- })
- .start()
- }
- protected onDisable(): void {
- Tween.stopAllByTarget(this.node);
- }
- update(deltaTime: number) {
-
- }
- }
|