Advertisement
Guest User

Corona SDK

a guest
Apr 27th, 2013
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.52 KB | None | 0 0
  1.  
  2. display.setStatusBar( display.HiddenStatusBar )
  3.  
  4. ----------------------------------------------- SELECTION MENU -----------------------------------------------
  5.  
  6. --> Make a rectangle the same size as the screen
  7. -- notice the use of "display.content_____", which gets the screen's height/width
  8. menuBg = display.newRect( 0, 0, display.contentWidth, display.contentHeight )
  9. menuBg:setFillColor( 16, 32, 99 )
  10.  
  11. --> Display the title logo, manually center it
  12. title = display.newImage("title.png")
  13. title.x = display.contentWidth/2
  14. title.y = display.contentHeight/2 - 75
  15.  
  16. --> Create the menu
  17. -- display.newRoundedRect( left, top, width, height, corner radius )
  18. menuBoxOuter = display.newRoundedRect( display.contentWidth/2 - title.x/2 , display.contentHeight/2, title.x, display.contentHeight/6, 30 )
  19. menuBoxOuter:setFillColor( 90, 117, 222 )
  20.  
  21. menuBoxInner = display.newRoundedRect( display.contentWidth/2 - title.x/2 + 5, display.contentHeight/2 + 5 , title.x - 10, display.contentHeight/6-10, 30 )
  22. menuBoxInner:setFillColor( 16, 32, 99 )
  23.  
  24. gameStart = display.newText( "Game Start (開始遊戲)", 0, 0, "Verdana", 20)
  25. gameStart:setTextColor( 90, 117, 222 )
  26. gameStart:translate( display.contentWidth/3, display.contentHeight/2+50 )
  27.  
  28. --> Create a display group for the menu
  29. local menu = display.newGroup()
  30. menu:insert( title )
  31. menu:insert( menuBoxOuter )
  32. menu:insert( menuBoxInner )
  33. menu:insert( gameStart )
  34.  
  35. function gameStart:tap( event )
  36. title:removeSelf()
  37. menu:remove( menuBoxOuter )
  38. menu:remove( menuBoxInner )
  39. menu:remove( gameStart )
  40. main()
  41. end
  42.  
  43. gameStart:addEventListener( "tap", gameStart )
  44.  
  45.  
  46.  
  47. function main( )
  48.  
  49. ----------------------------------------------- GRAPHICS, ART, BGM -----------------------------------------------
  50.  
  51.  
  52. --local bgm = audio.loadStream("tatakau.mp3")
  53. --audio.play(bgm)
  54.  
  55. local speedX = 2
  56. local speedY = 1
  57.  
  58. --> Add physics environment using standard Earth gravity
  59. local physics = require( "physics" )
  60. physics.start()
  61. physics.setGravity(0, 9.81)
  62.  
  63. --> Initialize background animations
  64.  
  65. -- 1. Data = define size and frames
  66. local bgData = { width=794, height=183, numFrames=2, sheetContentWidth=1588, sheetContentHeight=183 }
  67. -- 2. Sheet = define the sprite sheet
  68. local bgSheet = graphics.newImageSheet( "hkcAnim.png", bgData )
  69. -- 3. Animation = define animation characteristics
  70. local bgAnim = {
  71. { name = "bgAnimation", start = 1, count = 2, time = 800, loopCount = 0, -- repeat forever
  72. loopDirection = "forward"
  73. }
  74. }
  75. -- 4. Display the sprite (can't set size here unlike display.newImageRect)
  76. local bg = display.newSprite( bgSheet, bgAnim )
  77. -- 5. Center the animation
  78. bg:setReferencePoint( display.TopCenterReferencePoint )
  79. bg.x = display.contentCenterX
  80. bg.y = 0
  81. -- 6. Resize to match screen size based on a ratio with the actual background pixel dimensions
  82. bg:scale( display.contentWidth / 794, display.contentHeight / 446 )
  83. -- 7. Play the animation
  84. bg:play()
  85.  
  86. --> display.newRect( leftmost pixel, topmost pixel, width of rect, height of rect )
  87. local stage = display.newRect( 0, display.contentHeight * ( 183/446 ), display.contentWidth, display.contentHeight * ( 170/446 ) )
  88. stage:setFillColor( 157, 159, 161 )
  89. --physics.addBody( stage, "static" )
  90.  
  91. local floor = display.newImageRect( "hkc_floor.png", display.contentWidth, display.contentHeight * ( 93/446 ) )
  92. floor:setReferencePoint( display.BottomLeftReferencePoint )
  93. floor.x = 0
  94. floor.y = display.contentHeight
  95.  
  96. physics.addBody(floor, "static", { friction = 1.0, density = 1.0, bounce = 0.1 } )
  97.  
  98.  
  99. ----------------------------------------------- PLAYER -----------------------------------------------
  100.  
  101. local sequenceData =
  102. {
  103. { name="idle", start = 1, count = 4, time = 800, loopCount = 0, loopDirection = "forward" },
  104. { name="walking", start = 5, count = 4, time = 800, loopCount = 0, loopDirection = "forward" },
  105. { name="punching", start = 11, count = 4, time = 300, loopCount = 1, loopDirection = "forward" },
  106. { name="defending", start = 57, count = 1, time = 500, loopCount = 1 }
  107.  
  108. }
  109.  
  110. local davisData = { width = 80, height = 80, numFrames = 70, sheetContentWidth = 800, sheetContentHeight = 560 }
  111. local davisSheet = graphics.newImageSheet( "/Davis/davis0.png", davisData )
  112.  
  113. local davis = display.newSprite( davisSheet, sequenceData )
  114. davis.y = 500
  115. davis.x = 200
  116. charScale = display.contentHeight / 5
  117. charHeight = davis.height
  118. factor = charScale / charHeight
  119. davis:scale( factor, factor )
  120.  
  121. -- HP/MP bar
  122. local hp = display.newImage( "hp.png" )
  123. local mp = display.newImage( "mp.png" )
  124. local bar = display.newImage( "bar.png" )
  125. bar.y = davis.y - davis.height
  126. bar.x = davis.x
  127. hp.x = davis.x
  128. hp.y = bar.y - 4
  129. mp.x = davis.x
  130. mp.y = bar.y + 5
  131.  
  132. -- initially idle
  133. davis:setSequence( "idle" )
  134. davis:play()
  135.  
  136. ----------------------------------------------- ENEMY -----------------------------------------------
  137.  
  138. local sequenceData2 =
  139. {
  140. { name="idle", start = 1, count = 4, time = 800, loopCount = 0, loopDirection = "forward" },
  141. { name="walking", start = 5, count = 4, time = 800, loopCount = 0, loopDirection = "forward" },
  142. { name="punching", start = 11, count = 4, time = 300, loopCount = 1, loopDirection = "forward" },
  143. { name="hit", start = 47, count = 1, time = 500, loopCount = 1, loopDirection = "forward" },
  144. { name="defending", start = 57, count = 1, time = 500, loopCount = 1 }
  145.  
  146.  
  147. }
  148.  
  149. local dennisData = { width = 80, height = 80, numFrames = 70, sheetContentWidth = 800, sheetContentHeight = 560 }
  150. local dennisSheet = graphics.newImageSheet( "/Dennis/dennis_0.png", dennisData )
  151.  
  152. local dennis = display.newSprite( dennisSheet, sequenceData2 )
  153. dennis.y = 400
  154. dennis.x = 400
  155. dennis:scale( factor, factor )
  156.  
  157. -- HP/MP bar
  158. local hpEnemy = display.newImage( "hp.png" )
  159. local mpEnemy = display.newImage( "mp.png" )
  160. local barEnemy = display.newImage( "bar.png" )
  161. barEnemy.y = dennis.y - dennis.height
  162. barEnemy.x = dennis.x
  163. hpEnemy.x = dennis.x
  164. hpEnemy.y = barEnemy.y - 4
  165. mpEnemy.x = dennis.x
  166. mpEnemy.y = barEnemy.y + 5
  167.  
  168. -- initially idle
  169. dennis:setSequence( "idle" )
  170. dennis:play()
  171.  
  172. ----------------------------------------------- BUTTONS -----------------------------------------------
  173.  
  174. --> Initialize each button as not pressed
  175. local bUpPressed = false
  176. local bDownPressed = false
  177. local bLeftPressed = false
  178. local bRightPressed = false
  179. -- testing
  180. local bJumpPressed = false
  181.  
  182. --> Functions for moving the character
  183. local function moveCharacter(event)
  184. if bRightPressed then
  185. if( davis.x < display.contentWidth - 40 ) then
  186. davis.x = davis.x + speedX
  187. bar.x = bar.x + speedX
  188. mp.x = mp.x + speedX
  189. hp.x = hp.x + speedX
  190. end
  191. end
  192. if bUpPressed then
  193. if( davis.y > display.contentHeight * (183/446) - 40 ) then
  194. davis.y = davis.y - speedY
  195. bar.y = bar.y - speedY
  196. hp.y = hp.y - speedY
  197. mp.y = mp.y - speedY
  198. end
  199. end
  200. if bDownPressed then
  201. if( davis.y < display.contentHeight * (353/446) - 80 ) then
  202. davis.y = davis.y + speedY
  203. bar.y = bar.y + speedY
  204. hp.y = hp.y + speedY
  205. mp.y = mp.y + speedY
  206. end
  207. end
  208. if bLeftPressed then
  209. if( davis.x > 40 ) then
  210. davis.x = davis.x - speedX
  211. bar.x = bar.x - speedX
  212. hp.x = hp.x - speedX
  213. mp.x = mp.x - speedX
  214. end
  215. end
  216.  
  217. -------TESTING---------
  218.  
  219. if bJumpPressed then-- Using jump button to test movement
  220. -- AI always wants to move towards player
  221. if dennis.y > davis.y then
  222. print ("Move")
  223. dennis.y = dennis.y - speedY
  224. elseif dennis.y < davis.y then
  225. dennis.y = dennis.y + speedY
  226. print ("Move")
  227. end
  228.  
  229. if dennis.x > davis.x then
  230. dennis.x = dennis.x - speedX
  231. print ("Move")
  232. elseif dennis.y < davis.x then
  233. dennis.x = dennis.x + speedX
  234. print ("Move")
  235. end
  236.  
  237. barEnemy.y = dennis.y - dennis.height
  238. barEnemy.x = dennis.x
  239. hpEnemy.x = dennis.x
  240. hpEnemy.y = barEnemy.y - 4
  241. mpEnemy.x = dennis.x
  242. mpEnemy.y = barEnemy.y + 5
  243. end
  244. end
  245.  
  246. local bUp = display.newImage( "dpadsmall.png" )
  247. local bDown = display.newImage( "dpadsmall.png" )
  248. local bLeft = display.newImage( "dpadsmall.png" )
  249. local bRight = display.newImage( "dpadsmall.png" )
  250.  
  251. local bAtk = display.newImage( "atk1.png" )
  252. local bDef = display.newImage( "def1.png" )
  253. local bJump = display.newImage( "jump1.png" )
  254.  
  255. dPadSize = bUp.height
  256.  
  257. bUp:scale(0.5, 0.5)
  258. bUp.x = display.contentWidth / 10
  259. bUp.y = display.contentHeight * (3/4)
  260. bUp:addEventListener( "touch", bUp )
  261. bUp:toFront()
  262. Runtime:addEventListener( "enterFrame", moveCharacter )
  263.  
  264. bDown:scale(0.5, 0.5)
  265. bDown:rotate( 180 )
  266. bDown.x = display.contentWidth / 10
  267. bDown.y = display.contentHeight * (3/4) + 2 * (bDown.height*0.5)
  268. bDown:addEventListener( "touch", bDown )
  269. Runtime:addEventListener( "enterFrame", moveCharacter )
  270.  
  271. bLeft:scale(0.5, 0.5)
  272. bLeft:rotate( 270 )
  273. bLeft.x = display.contentWidth / 10 - bLeft.height*0.5
  274. bLeft.y = display.contentHeight * (3/4) + bLeft.height*0.5
  275. bLeft:addEventListener( "touch", bLeft )
  276. Runtime:addEventListener( "enterFrame", moveCharacter )
  277.  
  278. bRight:scale(0.5, 0.5)
  279. bRight:rotate( 90 )
  280. bRight.x = display.contentWidth / 10 + bRight.height*0.5
  281. bRight.y = display.contentHeight * (3/4) + bRight.height*0.5
  282. bRight:addEventListener( "touch", bRight )
  283. Runtime:addEventListener( "enterFrame", moveCharacter )
  284.  
  285. bDef:scale(0.2, 0.2)
  286. bDef.x = display.contentWidth * 15/20
  287. bDef.y = display.contentHeight * 9/10
  288. bDef:addEventListener( "touch", bDef )
  289.  
  290. bAtk:scale(0.2, 0.2)
  291. bAtk.x = display.contentWidth * 17/20
  292. bAtk.y = display.contentHeight * 8/10
  293. bAtk:addEventListener( "touch", bAtk )
  294.  
  295. bJump:scale(0.2, 0.2)
  296. bJump.x = display.contentWidth * 19/20
  297. bJump.y = display.contentHeight * 7/10
  298. bJump:addEventListener( "touch", bJump )
  299. Runtime:addEventListener( "enterFrame", moveCharacter )
  300.  
  301. ----------------------------------------------- STATE ANIMATIONS -----------------------------------------------
  302.  
  303. -- mirror checking for character
  304. local mirror = false
  305.  
  306. local function walking()
  307. davis:setSequence( "walking" )
  308. davis:play()
  309. end
  310.  
  311. local function idle()
  312. davis:setSequence( "idle" )
  313. davis:play()
  314. end
  315.  
  316. local function punching()
  317. davis:setSequence( "punching" )
  318. davis:play()
  319. -- Check if enemy (dennis) got punched by player (davis)
  320. distanceX = davis.x - dennis.x
  321. distanceY = davis.y - dennis.y
  322. print( "Distance = ", distanceX, distanceY )
  323. -- Reasonably close range required
  324. if ( distanceX >= -70 and distanceX <= 70 ) then
  325. if( distanceY >= -10 and distanceY <= 10 ) then
  326. print( "HIT!" )
  327. --barEnemy.isVisible = not(barEnemy.isVisible)
  328. dennis:setSequence( "hit" )
  329. dennis:play()
  330. hpEnemy.width = hpEnemy.width - 10
  331. end
  332. end
  333. end
  334.  
  335. local function defending()
  336. davis:setSequence( "defending" )
  337. davis:play()
  338. end
  339.  
  340.  
  341. --> Functions for moving the character
  342. local function moveCharacter(event)
  343. if bRightPressed then
  344. if( davis.x < display.contentWidth - 40 ) then
  345. davis.x = davis.x + speedX
  346. bar.x = bar.x + speedX
  347. end
  348. end
  349. if bUpPressed then
  350. if( davis.y > display.contentHeight * (183/446) - 40 ) then
  351. davis.y = davis.y - speedY
  352. bar.y = bar.y - speedY
  353. end
  354. end
  355. if bDownPressed then
  356. if( davis.y < display.contentHeight * (353/446) - 80 ) then
  357. davis.y = davis.y + speedY
  358. bar.y = bar.y + speedY
  359. end
  360. end
  361. if bLeftPressed then
  362. if( davis.x > 40 ) then
  363. davis.x = davis.x - speedX
  364. bar.x = bar.x - speedX
  365. end
  366. end
  367. end
  368.  
  369. ----------------------------------------------- CONTROLS -----------------------------------------------
  370.  
  371. function bUp:touch( event )
  372. if event.phase == "began" then
  373. walking()
  374. bUpPressed = true
  375. print( "Up" )
  376. return true
  377. elseif event.phase == "ended" then
  378. bUpPressed = false
  379. idle()
  380. return false
  381. end
  382. end
  383.  
  384. function bDown:touch( event )
  385. if event.phase == "began" then
  386. walking()
  387. bDownPressed = true
  388. print( "Down" )
  389. return true
  390. elseif event.phase == "ended" then
  391. bDownPressed = false
  392. idle()
  393. return false
  394. end
  395. end
  396.  
  397. function bLeft:touch( event )
  398. if event.phase == "began" then
  399. bLeftPressed = true
  400. walking()
  401. if mirror == false then
  402. davis:scale(-1,1)
  403. mirror = true
  404. end
  405. print( "Left" )
  406. return true
  407. elseif event.phase == "ended" then
  408. bLeftPressed = false
  409. idle()
  410. return false
  411. end
  412. end
  413.  
  414. function bRight:touch( event )
  415. if event.phase == "began" then
  416. bRightPressed = true
  417. print( "Right" )
  418. if mirror == true then
  419. davis:scale(-1,1)
  420. mirror = false
  421. end
  422. walking()
  423. return true
  424. elseif event.phase == "ended" then
  425. bRightPressed = false
  426. idle()
  427. return false
  428. end
  429. end
  430.  
  431. function bAtk:touch( event )
  432. if event.phase == "began" then
  433. print "Punch"
  434. punching()
  435. return true
  436. elseif event.phase == "ended" then
  437. idle()
  438. dennis:setSequence( "idle" )
  439. dennis:play()
  440. end
  441. end
  442.  
  443. function bDef:touch( event )
  444. if event.phase == "began" then
  445. print "Defend"
  446. defending()
  447. return true
  448. elseif event.phase == "ended" then
  449. idle()
  450. return false
  451. end
  452. end
  453.  
  454. function bJump:touch( event )
  455. if event.phase == "began" then
  456. bJumpPressed = true
  457. return true
  458. elseif event.phase == "ended" then
  459. bJumpPressed = false
  460. return false
  461. end
  462. end
  463.  
  464.  
  465.  
  466.  
  467. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement