-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathapp.js
More file actions
125 lines (95 loc) · 3.6 KB
/
app.js
File metadata and controls
125 lines (95 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
var window = Ti.UI.createWindow({backgroundColor:'black'});
// Obtain game module
var quicktigame2d = require('com.googlecode.quicktigame2d');
// Create view for your game.
// Note that game.screen.width and height are not yet set until the game is loaded
var game = quicktigame2d.createGameView();
// Frame rate can be changed (fps can not be changed after the game is loaded)
game.fps = 30;
// set initial background color to black
game.color(0, 0, 0);
game.debug = true;
// Create game scene
var scene = quicktigame2d.createScene();
// create new 64x64 shape
var shape = quicktigame2d.createSprite({width:64, height:64});
// color(red, green, blue) takes values from 0 to 1
shape.color(1, 0, 0);
// add your shape to the scene
scene.add(shape);
// add your scene to game view
game.pushScene(scene);
// Onload event is called when the game is loaded.
// The game.screen.width and game.screen.height are not yet set until this onload event.
game.addEventListener('onload', function(e) {
// Your game screen size is set here if you did not specifiy game width and height using screen property.
// Note that Ti.UI.View returns non-retina values (320x480) as view size,
// on the other hand the game screen property returns retina values (based on 640x960) on retina devices.
Ti.API.info("view size: " + game.size.width + "x" + game.size.height);
Ti.API.info("game screen size: " + game.screen.width + "x" + game.screen.height);
// Move your shape to center of the screen
shape.x = (game.screen.width * 0.5) - (shape.width * 0.5);
shape.y = (game.screen.height * 0.25) - (shape.height * 0.5);
// Start the game
game.start();
});
game.addEventListener('enterframe', function(e) {
// Move your shape
shape.x = shape.x + 2;
// Rotate your shape
shape.rotate(shape.angle + 6);
// If the shape moves outside of the screen,
// then the shape appears from the other side of the screen
if (shape.x + shape.width > game.screen.width) {
shape.x = -shape.width;
}
});
game.addEventListener('touchstart', function(e) {
var f = game.toImage();
Titanium.Media.saveToPhotoGallery(f, {
success: function(e) {
Titanium.UI.createAlertDialog({
title:'Photo Gallery',
message:'Saved'
}).show();
},
error: function(e) {
Titanium.UI.createAlertDialog({
title:'Error saving',
message:e.error
}).show();
}
});
/**
// We should calculate the view scale because Ti.UI.View returns non-retina values.
// This scale should be 2 on retina devices.
var scale = game.screen.width / game.width;
// Move your shape to center of the event position
shape.x = (e.x * scale) - (shape.width * 0.5);
shape.y = (e.y * scale) - (shape.width * 0.5);
**/
});
// Add your game view
window.add(game);
var centerLabel = Titanium.UI.createLabel({
color:'black',
backgroundColor:'white',
text:'touch screen to move rectangle',
font:{fontSize:20,fontFamily:'Helvetica Neue'},
textAlign:'center',
width:'auto',
height:'auto'
});
game.debug = true;
game.enableOnFpsEvent = true; // onfps event is disabled by default so enable this
game.onFpsInterval = 5000; // set onfps interval msec (default value equals 5,000 msec)
game.addEventListener('onfps', function(e) {
Ti.API.info(e.fps.toFixed(2) + " fps");
});
Ti.API.info("Label: " + centerLabel.width + "x" + centerLabel.height);
quicktigame2d.addEventListener('onlowmemory', function(e) {
Ti.API.warn("Low Memory");
});
// add label to the window
window.add(centerLabel);
window.open({fullscreen:true, navBarHidden:true});