zoukankan      html  css  js  c++  java
  • ccc prefab

    MonsterPrefab.js

    var Helpers = require('Helpers');
    
    cc.Class({
        extends: cc.Component,
    
        properties: {
            spriteList: {
                default: [],
                type: [cc.SpriteFrame]
            }
        },
    
        // use this for initialization
        onLoad: function () {
    
            //随机帧数
            var randomIdx = Helpers.getRandomInt(0, this.spriteList.length);
            var sprite = this.getComponent(cc.Sprite);
            sprite.spriteFrame = this.spriteList[randomIdx];
        }
    
    });
    
    

    Populate.js

    cc.Class({
        extends: cc.Component,
    
        properties: {
            root: {
                default: null,
                type: cc.Node
            },
            prefab: {
                default: null,
                type: cc.Prefab
            },
            canvas: {
                default: null,
                type: cc.Canvas
            },
            numberToSpawn: 0,
            spawnInterval: 0
        },
    
    
    
        // use this for initialization
        onLoad: function () {
            var self = this;
            self.randomRange = cc.p(300, 200);
            self.spawnCount = 0;
            self.schedule(self.addSpawn, self.spawnInterval);
        },
    
        addSpawn: function () {
            if (this.spawnCount >= this.numberToSpawn) {
                this.clearRepeater();
                return;
            }
    
            //初始化一个prefab
            var monster = cc.instantiate(this.prefab);
            monster.parent = this.root;
            //this.canvas.node.addChild(monster);
            monster.position = this.getRandomPosition();
            this.spawnCount++;
        },
    
        //随机一个位置
        getRandomPosition: function() {
            return cc.p(cc.randomMinus1To1() * this.randomRange.x, cc.randomMinus1To1() * this.randomRange.y);
        },
    
    
        //清理定时器
        clearRepeater: function() {
            this.unschedule(this.addSpawn);
        },
    });
    
    
  • 相关阅读:
    跟光磊学Python开发-面向对象入门
    插件调用下推操作
    K3Wise老单获取单据行数
    git 添加和删除 global 的 remote.origin.url
    CSV转Excel格式
    java 下载文件
    windows下安装redis并设置自启动
    linxu jdk安装
    Linux安装部署Redis
    CentOS上安装MySQL(使用国内源)
  • 原文地址:https://www.cnblogs.com/yufenghou/p/5446049.html
Copyright © 2011-2022 走看看