Created
March 20, 2012 07:46
-
-
Save h5y1m141/2132571 to your computer and use it in GitHub Desktop.
20120324-Titanium Nagoya Chatroom vol.2
This file contains 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
var win1 = Ti.UI.createWindow({backgroundColor:'black'}); | |
var totalScore = 0; | |
var scoreLabel = Titanium.UI.createLabel({ | |
top : 10, | |
left : 10, | |
height : 20, | |
width : 'auto', | |
color : 'white', | |
text : 'Score: ' + totalScore | |
}); | |
var quicktigame2d = require('com.googlecode.quicktigame2d'); | |
var game = quicktigame2d.createGameView(); | |
var scene = quicktigame2d.createScene(); | |
game.screen = {width:320, height:480}; | |
game.fps = 30; | |
game.color(0, 0, 0); | |
game.pushScene(scene); | |
var tank = quicktigame2d.createSprite({image:'images/tank.png'}); | |
tank.x = (game.screen.width/2) - (tank.width/2); | |
tank.y = game.screen.height - tank.height; | |
var back = quicktigame2d.createSprite({image:'images/back.png'}); | |
back.x = (game.screen.width/2) - (back.width/2); | |
back.y = 480 - back.height; | |
scene.add(back); | |
scene.add(tank); | |
var aliens = []; | |
var aliensSpeed = []; | |
var bullets = []; | |
var bulletsSpeed = []; | |
game.addEventListener('onload', function(e) { | |
initAliens(); | |
initBullet(); | |
game.start(); | |
}); | |
game.addEventListener('enterframe', function(e) { | |
bulletCollidesWithAliens(); | |
updateAliensPosition(); | |
updateBulletPosition(); | |
}); | |
game.addEventListener('touchmove',function(e){ | |
tank.x = e.x; | |
}); | |
win1.add(game); | |
win1.add(scoreLabel); | |
win1.open(); | |
function initAliens(){ | |
for (var i = 0; i < 5; i++) { | |
aliens[i]= quicktigame2d.createSprite({image:'images/alien1.png'}); | |
aliensSpeed[i] = 5; | |
aliens[i].x = Math.random() * game.screen.width; | |
aliens[i].y = -100; | |
scene.add(aliens[i]); | |
} | |
} | |
function initBullet(){ | |
for(var j=0;j<10;j++){ | |
bullets[j]= quicktigame2d.createSprite({image:'images/bullet.png'}); | |
bullets[j].x = initBulletsPostion(); | |
bullets[j].y = tank.y - (bullets[j].height); | |
bulletsSpeed[j] = 10; | |
scene.add(bullets[j]); | |
} | |
} | |
function initBulletsPostion(){ | |
return tank.x + (tank.width/2) -(bullets[0].width/2); | |
} | |
function updateAliensPosition(){ | |
for (var i = 0; i < 5; i++) { | |
aliens[i].y += aliensSpeed[i] * Math.random(); | |
if(aliens[i].y > 480){ | |
aliens[i].y = -100; | |
} | |
} | |
} | |
function updateBulletPosition(){ | |
for (var i = 0; i < 10; i++) { | |
bullets[i].y -= bulletsSpeed[i]; | |
if(bullets[i].y < 0 || bullets[i].y > 480){ | |
bullets[i].x = initBulletsPostion(); | |
bullets[i].y = tank.y - (bullets[i].height); | |
} | |
} | |
} | |
function bulletCollidesWithAliens(){ | |
for (var i = 0; i < 10; i++) { | |
for(var j=0;j<5;j++){ | |
var flg = bullets[i].collidesWith(aliens[j]); | |
if(flg){ | |
totalScore +=100; | |
scoreLabel.text = ('Score:' + totalScore); | |
aliens[j].y = -100; | |
bullets[i].x = initBulletsPostion(); | |
bullets[i].y = tank.y - (bullets[i].height); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment