Skip to content

Instantly share code, notes, and snippets.

@philipshen
Last active June 24, 2018 04:33
Show Gist options
  • Save philipshen/0ad7f46d16c1f7042b9b2729041c629b to your computer and use it in GitHub Desktop.
Save philipshen/0ad7f46d16c1f7042b9b2729041c629b to your computer and use it in GitHub Desktop.
@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