###わからないこと
以前書いたjsのコードをrailsに移すとき、せっかくならcoffeeに書き直してみようと考え、頑張ってみましたがうまくいきません。
coffeescriptのことはよくわからず、どこがまちがっているのかわかりません。
どなたか教えてくれると助かります。
###エラー
SyntaxError: [stdin]:7:1: unexpected indentation このようなエラーが出ていました
↓
(追記)今はこんなエラーが出ています。
SyntaxError: [stdin]:8:18: unexpected ,
↓
エラー直りました!
###ソースコード
window.onload = -> draw() draw = -> canvas = document.getElementById('canvas') if !canvas || !canvas.getContext then return false ctx = canvas.getContext('2d') start=0 end=0 balls=[] schottBalls=[] direction=['top','right','left','bottom'] rand = (min,max)-> return min+Math.floor(Math.random()*(max-min+1)) Ball = (name, direction) -> this.x = rand(100, 300) this.y = rand(100, 200) this.r = 10 this.name = name this.direction = direction if name == '' then this.name = 'enemy' Ball.prototype.draw = -> if this.direction == 'top' start = 290 end = 250 else if this.direction == 'left' start = 200 end = 160 else if this.direction == 'bottom' start = 110 end = 70 else if this.direction == 'right' start = 20 end = 340 ctx.beginPath() ctx.arc(this.x, this.y, this.r, start / 180 * Math.PI, end / 180 * Math.PI) ctx.stroke() checkPosition =(x, y, r, ball) -> if x - r < 0 ball.x = r else if x + r > canvas.width ball.x = canvas.width - r else if y - r < 0 ball.y = r else if y + r > canvas.height ball.y = canvas.height - r stop = (a, b, c) -> switch a when 1 return msg = if (b - c) < 0 then 5 else -5 when 2 return msg = if (b + c) > canvas.width then -5 else 5 when 3 return msg = if (b - c) < 0 then 5 else -5 when 4 return msg = if (b + c) > canvas.height then -5 else 5 mobMove = -> for i in [1..balls.length-1] enemyBall = balls[i] if enemyBall.name == 'enemy' a = rand(1, 4) switch a when 1 enemyBall.direction = 'left' enemyBall.x += stop(1, enemyBall.x, enemyBall.r) break when 2 enemyBall.direction = 'right' enemyBall.x += stop(2, enemyBall.x, enemyBall.r) break when 3 enemyBall.direction = 'top' enemyBall.y += stop(3, enemyBall.y, enemyBall.r) break when 4 enemyBall.direction = 'bottom' enemyBall.y += stop(4, enemyBall.y, enemyBall.r) break checkPosition(enemyBall.x, enemyBall.y, enemyBall.r, enemyBall) window.onkeydown = -> k = event.keyCode userBall = balls[0] if k == 37 userBall.x -= 5 userBall.direction = 'left' else if k == 38 userBall.y -= 5 userBall.direction = 'top' else if k == 39 userBall.x += 5 userBall.direction = 'right' else if k == 40 userBall.y += 5 userBall.direction = 'bottom' checkPosition(userBall.x, userBall.y, userBall.r, userBall) draw = -> requestAnimationFrame(draw) ctx.clearRect(0, 0, canvas.width, canvas.height) frame++ if !(frame % 10) mobMove() for i in [0..balls.length-1] balls[i].draw() while i = schottBalls.length schottBalls[i].draw() i-- SchottBall =(x, y, w)-> this.x = x this.y = y this.w = w SchottBall.prototype.draw = -> ctx.beginPath() ctx.arc(this.x, this.y, 5, 0, 2 * Math.PI) ctx.stroke() switch this.w when 'top' this.y -= 3 when 'right' this.x += 3 when 'left' this.x -= 3 when 'bottom' this.y += 3 if this.x >= canvas.width + 5 || this.x <= -5 || this.y >= canvas.height + 5 || this.y <= -5 schottBalls.splice(schottBalls.indexOf(this), 1) frame = 0 init = -> balls.push(new Ball('user', direction[rand(0, 4)])) for i in [0..2] balls.push(new Ball('', direction[rand(0, 4)])) draw() init() window.onkeyup = (e)-> if e.keyCode == 32 for i in [0..balls.length-1] ball = balls[i] if ball.name == 'user' if ball.direction == 'top' schottBalls.push(new SchottBall(ball.x, ball.y - ball.r, 'top')) else if ball.direction == 'left' schottBalls.push(new SchottBall(ball.x - ball.r, ball.y, 'left')) else if ball.direction == 'bottom' schottBalls.push(new SchottBall(ball.x, ball.y + ball.r, 'bottom')) else if ball.direction == 'right' schottBalls.push(new SchottBall(ball.x + ball.r, ball.y, 'right'))
###その他
本文が決められている文字数を超えるのでもとのjsのコードは載せれませんでした。以前の質問に載せてあります

回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/02/04 01:05