Last active
June 24, 2018 04:33
-
-
Save philipshen/0ad7f46d16c1f7042b9b2729041c629b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@ccclass | |
export default class Game extends cc.Component { | |
@property(cc.Prefab) | |
meteor: cc.Prefab = null | |
start() { | |
this.scheduleCreateMeteor() | |
} | |
createMeteor() { | |
const x = this.randInRange(this.meteorSpawnMinX, this.meteorSpawnMaxX) | |
const y = this.randInRange(this.meteorSpawnMinY, this.meteorSpawnMaxY) | |
const angle = Math.random() * 360 | |
const velocity = this.randInRange(this.meteorMinVelocity, this.meteorMaxVelocity) | |
const node = cc.instantiate(this.meteor) | |
node.setPosition(cc.v2(x, y)) | |
const body = node.getComponent(cc.RigidBody) | |
body.linearVelocity = cc.v2(MathUtilities.sind(angle) * velocity, MathUtilities.cosd(angle) * velocity) | |
this.node.addChild(node) | |
} | |
scheduleCreateMeteor() { | |
cc.director.getScheduler().schedule(this.createMeteor, this, 1 + Math.random(), false); | |
} | |
// UTILITIES | |
randInRange(min: number, max: number): number { | |
return Math.random() * (max - min) + min; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment