Class game Extends App error
Monkey Forums/Monkey Beginners/Class game Extends App error
| ||
I am using the Michael Hartlef book and his Air Dogs 1942 code generates this error upon build. Any ideas anyone...please. I have exhausted all possibilities. Import fantomEngine Global g:game '*************************************** Class game Extends App Field eng:engine Field isSuspended:Bool = False Field txtScore:ftObject Field txtScoreC:ftObject Field txtYouWin:ftObject Field txtYouLoose:ftObject |
| ||
What errors? Is that the entire source code? |
| ||
Thank you for asking: Hi, the entire code is as follows. I am trying to recreate it, but haven't finished. Any ideas. Nothing seems to run from his book code files! Strict #rem Script: airdogs1942.monkey Description: Sample script from chapter #8 of the book "Monkey Game Development Beginners guide" by PacktPub Author: Michael Hartlef #end Import fantomEngine Global g:game '*************************************** Class game Extends App Field eng:engine Field isSuspended:Bool = False Field txtScore:ftObject Field txtScoreC:ftObject Field txtYouWin:ftObject Field txtYouLoose:ftObject Field player:ftObject Field enemy:ftObject Field font1:ftFont Field layerBackground:ftLayer Field layerGame:ftLayer Field layerClouds:ftLayer Field layerInfo:ftLayer Field layerTitle:ftLayer Field sndHit:ftSound Field sndExplo:ftSound Field sndShoot:ftSound Field sndEngine:ftSound Field atlas:Image Field gameMode:Int = gmMenu Field score:Int = 0 Field scoreC:Int = 0 Field hits:Int = 0 Field hitsC:Int = 0 Field cw:Float = 0.0 Field ch:Float = 0.0 Field canShoot:Bool=True '------------------------------------------ Const gmMenu:Int = 1 Const gmPlay:Int = 2 Const gmGameOver:Int = 3 Const grpPlayer:Int = 1 Const grpEnemy:Int = 2 Const grpShot:Int = 3 Const triDelete:Int = 6 Const tmDelete:Int = 15 Const tmCanShoot:Int = 16 '------------------------------------------ Method LoadSounds:Int() sndHit = eng.LoadSound("hit") sndExplo = eng.LoadSound("explosion") sndShoot = eng.LoadSound("shoot") 'Load the engine sound with the LOOP parameter set to true sndEngine = eng.LoadSound("engine",True) Return 0 End '------------------------------------------ Method CreateLayers:Int() layerBackground = eng.GetDefaultLayer() layerGame = eng.CreateLayer() layerClouds = eng.CreateLayer() layerInfo = eng.CreateLayer() layerTitle = eng.CreateLayer() Return 0 End '------------------------------------------ Method CreateBackgroundScreen:Int() eng.SetDefaultLayer(layerBackground) Local obj:= eng.CreateImage("background.png",cw/2,ch/2) Return 0 End '------------------------------------------ Method CreateClouds:Int() Local obj:ftObject Local shape:Float 'Set the default layer eng.SetDefaultLayer(layerClouds) For Local i:Int = 1 To 10 'Determine the cloud shape shape = Rnd(100.0) If shape <= 20.0 Then obj = eng.CreateImage(atlas,128,0,64,64,Rnd(10,cw-10),Rnd(10,ch-10)) Elseif shape > 20.0 And shape <= 70.0 Then obj = eng.CreateImage(atlas,192,0,64,64,Rnd(10,cw-10),Rnd(10,ch-10)) Else obj = eng.CreateImage(atlas,0,64,128,128,Rnd(10,cw-10),Rnd(10,ch-10)) Endif 'Set its speed and angle obj.SetSpeed(Rnd(0.2,2),Rnd(360)) obj.SetAngle(Rnd(360)) 'Make the cloud wrap around the screen edges obj.SetWrapScreen(True) Next Return 0 End '------------------------------------------ Method CreateInfoText:Int () eng.SetDefaultLayer(layerInfo) txtScore = eng.CreateText(font1,"Player: "+score,10,0) txtScoreC = eng.CreateText(font1,"Computer: "+scoreC,cw-10,0,2) txtYouWin = eng.CreateText(font1,"YOU WIN :=)",cw/2,ch/2,1) txtYouLoose = eng.CreateText(font1,"YOU LOOSE :-(",cw/2,ch/2,1) Return 0 End '------------------------------------------ Method CreateTitleScreen:Int () eng.SetDefaultLayer(layerTitle) Local box:ftObject = eng.CreateBox(cw,ch,cw/2,ch/2) Local tx1:ftObject = eng.CreateText(font1,"Air Dogs 1942",cw/2,ch/4,1) tx1.SetScale(2.0) Local tx2:ftObject = eng.CreateText(font1,"Press 'P' to play",cw/2,ch/2+20, 1) Local tx3:ftObject = eng.CreateText(font1,"Press 'ESC' to end the game", cw/2,ch/2+120,1) Local p1:ftObject = eng.CreateImage(atlas,0,0,64,64,cw/2-ch/8,ch/6) p1.SetAngle(45) Local p2:ftObject = eng.CreateImage(atlas,64,0,64,64,cw/2+ch/8,ch/6) p2.SetAngle(315) Return 0 End '------------------------------------------ Method SpawnPlayer:Int () player = eng.CreateImage(atlas,0,0,64,64,cw/4,ch/2) player.SetScale(0.7) player.SetSpeed(8) player.SetWrapScreen(True) player.SetColGroup(grpPlayer) player.SetRadius(24) hits=0 Return 0 End '------------------------------------------ Method SpawnEnemy:Int () enemy = eng.CreateImage(atlas,64,0,64,64,cw/4*3,ch/2) enemy.SetScale(0.7) enemy.SetAngle(180) enemy.SetSpeed(8) enemy.SetWrapScreen(True) enemy.SetColGroup(grpEnemy) enemy.SetRadius(24) hitsC=0 canShoot=True Return 0 End '------------------------------------------ Method StartNewGame:Int() Seed = Millisecs() score = 0 scoreC = 0 layerTitle.SetActive(False) layerGame.RemoveAllObjects() eng.SetDefaultLayer(layerGame) SpawnPlayer() SpawnEnemy() gameMode = gmPlay txtYouWin.SetActive(False) txtYouLoose.SetActive(False) sndEngine.Play() Return 0 End '------------------------------------------ Method SpawnSmoke:Int(plane:ftObject, hits:Int) 'Determine if a smoke particle will show up. The chance is higher the more hits the plane has If Rnd(0,100)>(100-hits*10) Then eng.SetDefaultLayer(layerGame) 'Calculate the vector of the new smoke object. Local vec:Float[] = plane.GetVector(20,180+Rnd(-10*hits,10*hits),True) 'Determine the offset from the sprite atlas for the image Local xoff:Float = Int(Rnd(0,3)) Local obj:=eng.CreateImage(atlas,144+xoff*16.0,64,16,16,vec[0],vec[1]) 'Fade out the particle over time. Local trans:ftTrans = obj.CreateTransAlpha(0.1, Rnd(1000,3000), False, triDelete) obj.SetScale(Rnd(0.1,0.5)) 'Scale up the particle too obj.AddTransScale(trans, Rnd(1.0,4.0), False) Endif Return 0 End '------------------------------------------ Method SpawnExplosion:Int(x:Float, y:Float, amount:Int) eng.SetDefaultLayer(layerGame) For Local i:Int = 1 To amount 'Determine the offset from the sprite atlas for the image Local xoff:Float = Int(Rnd(0,3)) Local obj:=eng.CreateImage(atlas,144+xoff*16.0,80,16,16,x,y) 'Determine the target vector Local vec:Float[] = obj.GetVector(Rnd(10,40),Rnd(360)) 'Move it to the vector over time Local trans:ftTrans = obj.CreateTransPos(vec[0], vec[1], Rnd(1000,3000), False, triDelete) obj.SetScale(Rnd(0.1,1.0)) 'Scale up the particle too obj.AddTransScale(trans, Rnd(2.0,4.0), False) 'Fade out the particle over time. obj.AddTransAlpha(trans, 0.5, False) Next Return 0 End '------------------------------------------ Method SpawnShot:Int (plane:ftObject) If (plane = enemy And canShoot = True) Or plane=player Then Local vec:Float[] = plane.GetVector(40,0,True) Local s:ftObject = eng.CreateImage(atlas,128,64,12,16,vec[0],vec[1]) s.SetAngle(plane.GetAngle()) s.SetSpeed(15) s.SetWrapScreen(True) s.SetColGroup(grpShot) s.SetColWith(grpPlayer, True) s.SetColWith(grpEnemy, True) s.SetRadius(6) 'Let the bullet be automatically deleted after 2 seconds eng.CreateObjTimer(s,tmDelete,2000) If plane = enemy Then canShoot = False 'Set canShoot to TRUE after 300 milliseconds eng.CreateObjTimer(plane,tmCanShoot,300) Endif sndShoot.Play() Endif Return 0 End '------------------------------------------ Method FollowPlayer:Int(obj:ftObject) 'Determine the relative angle to the player Local angle:Float = obj.GetTargetAngle(g.player,True) If angle < 0 Then obj.SetAngle(-eng.GetDeltaTime()/16.0,True) Else obj.SetAngle(eng.GetDeltaTime()/16.0,True) Endif 'Shot at the player if it is in sight If Abs(angle) < 10.0 Then SpawnShot(obj) obj.SetSpeedAngle(obj.GetAngle()) Return 0 End '------------------------------------------ Method OnCreate:Int() SetUpdateRate(60) eng = New engine eng.SetCanvasSize(800,600) ch = eng.canvasHeight cw = eng.canvasWidth atlas = LoadImage("ad_Tiles.png") font1 = eng.LoadFont("ad_font") LoadSounds() CreateLayers() CreateBackgroundScreen() CreateClouds() CreateInfoText() CreateTitleScreen() Return 0 End '------------------------------------------ Method OnUpdate:Int() Local d:Float = Float(eng.CalcDeltaTime())/60.0 If isSuspended = False Then Select gameMode Case gmPlay eng.Update(Float(d)) eng.CollisionCheck(layerGame) If KeyHit(KEY_ESCAPE) Then layerTitle.SetActive(True) sndEngine.Stop() gameMode = gmMenu Endif txtScore.SetText("Player: "+score) txtScoreC.SetText("Computer: "+scoreC) Case gmMenu If KeyHit(KEY_P) Then StartNewGame() Endif If KeyHit(KEY_ESCAPE) Then Error ("") Endif Case gmGameOver If TouchHit(0) Or KeyHit(KEY_ESCAPE) Then gameMode = gmMenu layerTitle.SetActive(True) Endif End Endif Return 0 End '------------------------------------------ Method OnRender:Int() Cls eng.Render() Return 0 End '------------------------------------------ Method OnResume:Int() isSuspended = False SetUpdateRate(60) Return 0 End '------------------------------------------ Method OnSuspend:Int() isSuspended = True SetUpdateRate(5) Return 0 End End '*************************************** Function Main:Int() g = New game Return 0 End '*************************************** Class engine Extends ftEngine '------------------------------------------ Method OnObjectTimer:Int(timerId:Int, obj:ftObject) If timerId = g.tmDelete Then obj.Remove() If timerId = g.tmCanShoot Then g.canShoot = True Return 0 End '------------------------------------------ Method OnObjectTransition:Int(transId:Int, obj:ftObject) If transId = g.triDelete Then obj.Remove() Return 0 End '------------------------------------------ Method OnObjectCollision:Int(obj:ftObject, obj2:ftObject) If obj.GetColGroup()=g.grpShot Then g.sndHit.Play() If obj2 = g.player Then g.hits += 1 If g.hits >= 5 Then g.scoreC += 1 g.sndExplo.Play() g.SpawnExplosion(obj.GetPosX(), obj.GetPosY(),15) obj2.Remove() g.SpawnPlayer() Endif Endif If obj2 = g.enemy Then g.hitsC += 1 If g.hitsC >= 5 Then g.score += 1 g.sndExplo.Play() g.SpawnExplosion(obj.GetPosX(), obj.GetPosY(),15) obj2.Remove() g.SpawnEnemy() Endif Endif obj.Remove() Endif Return 0 End '------------------------------------------ Method OnObjectUpdate:Int(obj:ftObject) If obj = g.player Then obj.SetSpeed(8.0) If KeyDown(KEY_LEFT) Then obj.SetAngle(-g.eng.GetDeltaTime()/16.0,True) If KeyDown(KEY_RIGHT) Then obj.SetAngle(g.eng.GetDeltaTime()/16.0,True) If KeyDown(KEY_UP) Then obj.SetSpeed(10.0) If KeyDown(KEY_DOWN) Then obj.SetSpeed(6.0) If KeyHit(KEY_S) Then g.SpawnShot(obj) If g.hits > 0 Then g.SpawnSmoke(obj, g.hits) Endif If obj = g.enemy Then g.FollowPlayer(obj) If g.hitsC > 0 Then g.SpawnSmoke(obj, g.hitsC) Endif Return 0 End '------------------------------------------ Method OnLayerUpdate:Int(layer:ftLayer) If g.score >= 5 Then g.txtYouWin.SetActive(True) g.gameMode = g.gmGameOver g.sndEngine.Stop() Elseif g.scoreC >= 5 Then g.txtYouLoose.SetActive(True) g.gameMode = g.gmGameOver g.sndEngine.Stop() Endif Return 0 End End |
| ||
Obviously not indented! |
| ||
I think Monkey was changed quite a bit after Michael wrote his book. What compilation errors are you getting? You can wrap the code in a codebox with the tags [ codebox ] test [/ codebox]: (But without the spaces) |
| ||
Hi Fingy, yes. Quite a bit has changed since I wrote it. Which version of monkey are you running? What error do you got? Edit: Just checked. The latest code version I sent to Pack runs with the latest Monkey and fantomEngine versions. For the latest version of fantomEngine, visit http://www.fantomgl.com |
| ||
Ok, I got it. You wrote the error in the title. :-) Usually you get such error when Monkey can't find a module. In this case fantomEngine. How is your folder structure? Where did you save the framerwork? |
| ||
Hi - great book thanks! I am using the free version 84f. I have save d the files locally with the data files and monkey file together. Other programs have worked for me, but not these. Hopefully it is something simple. I am new to this, so I bet it is a doddle to you! What might I be doing wrong? Many thanks Michael |
| ||
I Fingy, Glad you like the book. Yes, it is something simple. The Monkey translator can't find the fantomEngine files on your computer.I am at work at the moment. Tonight when I am back home, I will write detailed instruction again. Also check if there is a problem with the free version. Regarding the installation of fantomEngine. The modules fantomEngine/JSON/Box2D should be copied either to a location you also configurize in your monkey config file, or inside the modules_ext folder of your monkey installation. Not in the folder where the game script files are. I hope I got the names correct as I have no access to my files from here. Like I said, I will write more details later if you still need to. |
| ||
Btw. the first to examples, PONG and Rocket Commander should run as they use only pure Monkey code and not the fantomEngine framework. Did you got the physical book or an ebook? EDIT: As I am not sure if Paxckt published the errata I send them, look here for them http://forum.fantomgl.com/viewforum.php?f=27 |
| ||
Hi Michael I have successfully installed fantomX and the next error is Self.pathTime = Millisecs() [ codebox ]Self.pathTime = Millisecs() [/ codebox]: Therfore still not running? I checked your blog for code changes to chapter 8 but could not see anything. Many thanks for your advice. Not sure if my code box has worked until I press reply! |
| ||
Forgot to say, it is the hard copy, not the ebook if it makes a difference. Regards Michael |
| ||
fantomX? The book is based on fantomEngine. And the samples don't work with fantomX. I have successfully installed fantomX and the next error is Self.pathTime = Millisecs() Which file, which line?. What error message? Please be more specific. |
| ||
Btw. Which version of fantomEngine or fantomX did you download? |
| ||
Hi Your post took me to http://www.fantomgl.com/ I could only see one folder to download. In Airdogs, line 344 according to the console Error message: Compile error Identifier milliseconds not found Thanks :) |
| ||
The newest version of fantomEngine can be found here: http://forum.fantomgl.com/viewtopic.php?f=32&t=162 Are the example scripts running for you? Your compile error makes no sense and again, it seems not be the complete outpu in the consolet. It normally states the file name with it. In the Airdogs1942.monkey file there is no reference to this line of coding at all. Pleaee copy and paste the output of the console here. Did you type ther code of chapter 8 yourself from the book or did you download the sources from PacktsPub? If you typed it yourself, please let me know which page number you found that line of code? |
| ||
Hi No, they are not all running yet. The console output is: "C:/MonkeyXFree84f/bin/transcc_winnt" -target=Html5_Game -config=Debug -run "C:/Users/User/Documents/9781849692038_code/2038_Code_New/Chapter 8/airdogs1942.monkey" TRANS monkey compiler V1.85 Parsing... Semanting... C:/Users/User/Documents/9781849692038_code/2038_Code_New/Chapter 8/airdogs1942.monkey<14> : Error : Type 'App' not found Done I wanted to see what the game did and this is your code, and I am also typing it myself but not yet finished. I bet it is something simple! Many thanks Michael |
| ||
C:/Users/User/Documents/9781849692038_code/2038_Code_New/Chapter 8/airdogs1942.monkey<14> : Error : Type 'App' not found Ok. Looks like Monkey can't find the fantomEngine module. I thought you had that fixed as your message above made me assume that. Please describe where you have copied the framework, the JSON module and the Box2D module. By default Moneky first looks in the modules folder and in the modules_ext folder. You can also place it inside a different folder but then you need to specify this inside the config file. The most easiest way is to place new modules inside the module_ext folder which comes with your monkey installation. |
| ||
Here is a little slideshow that will illustrate, where fantomEngine and the 2 other modules have to be installed and how to test if the installation was successfull: http://fedocs.fantomgl.com/slideshow/fantomEngine.htm |
| ||
Hi Mike Apologies for the late reply. It is all sorted and the game is running - and it is very good! Many thanks for all your help. No doubt I will be back on to you at some point. Best wishes Michael - new MonkeyX addict! |
| ||
I am glad you got it running. |