Introduction to CocosSharp: 2D Game Development in C#
Montemagno, James
C# has become one of the most prolific languages used in game development. Games developed in C# can be created to target desktops, tablets, mobile devices and game consoles, while sharing nearly the entire code base. On mobile devices 2D games in particular are massively popular. In this session, we'll explore how to use C# in conjunction with Microsoft and Xamarin to develop 2D games that will work on iOS, Android and Windows platforms. We'll cover the basics of 2D game development, working with multiple scenes, animation and even physics. By the end of the session, you'll have seen how to make a cross-platform 2D game that you can use as the basis for creating your own games.
 Awesome look at that fancy transition. Whoo. That was awesome. So how many of your C sharp developers? Awesome, you're all game developers that just happened. I just turned you all into game developers How many of you guys have ever made games at all like 2d3d? Quite a few how about like for councils a Few all right you guys win So a long time ago 2006 I lived in Phoenix, Arizona And I wrote a lot of C++ code and I made a game for the Xbox 360 I had it dive deep into the Facebook archives actually find it and a little video. It's an FLV Whenever that was made so here's my history of video games So I did this with super sweet music So that took about two years and I did that for a small company and then I said okay I'm never gonna do that again Like that was the worst experience in my life who wants to write C++ code over and over and over again So I learned C sharp and then I wrote printer software for four years games to printer software natural transition It's fantastic. I really like to say that you lead need at least four years of Enterprise printer software under your belt to put software engineer on your LinkedIn page It's just a given like you have to have it. So what happened? Well, and who am I? Well, that's me. I'm James Montemagno I'm a developer evangelist at Xamarin I've been creating and crafting dotnet applications for the last decade after ever since I moved out of the game industry And I live in this wonderful place. It's just like Oslo, but it's called Seattle. It's lovely. It rains there, too We got that it's cloudy, but it's also beautiful I'm blown away when I got off the plane my girlfriend says how's Oslo? I said it's just like home and it's actually really true great coffee great people just actually really really beautiful But I moved there four years ago because I said enough is enough with printer software Because really you can only take about four years of printer software any software developers writing printer software right now Perfect you all you all nailed it never do that never do that It's actually it's not that bad you learn great enterprise like when you know are porting a legacy dotnet 11 application to 2.0 to 3.5. You're like, that's great So I said enough is enough. I'm gonna move to Seattle not only because we have the most amazing coffee everywhere besides Oslo Maybe I'm going to Tim's tomorrow. I'm very excited about that like I came to Oslo I was like, what do I need to do coffee like oh go to Tim's very excited But I moved there because I love mobile development. That's been my main focus I went to the final PDC before there was fancy builds and everything like that got my hands on a Windows phone I was like, oh my goodness there's so much power in the palm of my hand and I found Xamarin because I was a C sharp developer And I've been crafting iOS and Android and Windows applications for the last four years and every single thing that I do Is open source complete on my github? I can find me anytime on github on my blog at James as Xamarin.com or really just on Twitter That's the easiest way to get a hold of me. I'm up probably 24 hours a day probably on a plane somewhere flying somewhere I like to say I code 50% of the time on a Plane 50% of the time presenting 50% of the time and helping all of you guys 50% of the time Create create awesome mobile applications. So I'm kind of 200% always on so we're here to talk about Cocoa sharp and building 2d games all in C sharp I'm gonna tell you a little bit about Xamarin. How many people have no idea what Xamarin is a Few per is always like one or two so Xamarin I have a talk later on Xamarin itself with Xamarin forms building like native Applications for iOS Android of course windows all in C sharp And the Xamarin platform is kind of actually what powers Cocoa sharp in a way It's enabling us as C sharp and dotnet developers to get away and still create windows applications But come over to iOS Android and Mac and use all of our dotnet and C sharp skills and libraries to create great native Applications that's a Xamarin platform. You can do that in visual studio or Xamarin studio RIDE We also have a great testing product called Xamarin test cloud to do automated user interface testing for any of your iOS or Android apps Across thousands of physical devices. We even have a crash of litics and analytics software for your mobile applications and Entire training online university called Xamarin University go figure It has a free trial actually so you can actually go take a few classes for free and learn all about Xamarin and creating iOS and Android apps And how this all works and kind of even how the Cocoa sharp bits works is the same way that Xamarin works We need to build out a shared C sharp back end for any of your mobile applications And these are all your business logic of your application. So your models your view models Your SQLite databases your calls to Azure to AWS or wherever you want to go any of your dotnet code your things that your Applications are doing the same on each platform Then we give you the tools or frameworks to build on native user interfaces for iOS Android and Windows now I usually talk about creating just mobile applications themselves either for the enterprise or consumer But you can think about this iOS and Android and Windows UI up top there as just a game UI and that's where the Cocoa sharp libraries gonna come in and replace that Native kind of storyboard or Android XML with actual 2d game engines and user interface So that's what we're gonna talk about today And there's actually several ways of building games with Xamarin not only just Cocoa sharp So if you want to tie into the actual native platforms or in 3d as well, there's ways of doing that I like Cocoa sharp Because I never thought I'd go back to games and then I went to a game jam and created a game in like 12 hours And I was blown away and I'll show you guys that later on But we have mono game and that's actually the core of actual Development it's a it's a it's a port of XNA over to iOS Android Mac and even Linux that powers tons of amazing games like Bastion and Towerfall and a bunch of amazing games out even on like PlayStation like transistor for instance So that's all in C sharp. We also have a mono for unreal, which is a project that we've been working on It's just for desktop at this point in time But if you're kind of in the unreal engine type of space, there's mono for for unreal so you can write everything in C sharp Which is very cool Built into core iOS is a few API's that Apple gave to actually create Native 2d games and sprite kit is one of those if you use Xamarin to actually create your native iOS and Android applications You get access to all the API So if you could do it in objective C or in Java and you can do it in C sharp So we expose sprite kit and also scene kit as a way to build 2d and 3d games just for iOS But I think Cocoa sharp is kind of like a sweet spot in between because it blends the paradigms of what we kind of want to do in 2d games with C sharp idioms and Miguel kind of wrapped it up like when people ask what is Cocoa sharps? And he says it blends the power of the Cocoa's 2d Programming model with C sharp and the dot net framework. So leveraging C sharp and dot net And then what we did is we took what we kind of loved about Cocoa's 2d And we added the C sharp and dot net idioms on top of it So if you ask Miguel why why did we create C sharp or Cocoa sharp as part of Xamarin? Well, this is why because we love the ability to kind of create 2d games with Cocoa's 2d But we wanted to do it in C sharp in a way that dot net developers would kind of know and love with events and and all the great Things about dot net in the back end. So how many people are familiar with Cocoa's 2d at all? Nobody, okay, cool So well, let me tell you a little bit about the history of where Cocoa's sharp came from Well, Cocoa's 2d is a 2d game engine and it was built in object for dejective C developers So they create Mac and iOS applications And then that got ported into the cross platform space with a Cocoa's 2dx Which was all C++ based? Well, that's that's great. Everyone loves to manage memory So then someone said there's got to be a better way So someone then went out and leveraged monogame Did already be out and created Cocoa's 2dx na which was all in C sharp But it leveraged new platforms across iOS and Android So the first two there the first one only targeted iOS and then Cocoa's 2dx targeted Android and iOS and even Mac desktop and then Cocoa's 2dx na which has been out for quite a long time Targeted all the platforms Now what we did with Cocoa's sharp was that we took what was there in Cocoa's 2dx na We kind of loved what was going on there. Well, we wanted to contribute actively to it So we kind of forked it and there's some logistics of putting things back into a different licensing So we said, okay, we're just gonna open source all of Cocoa's sharp So it's available completely on github We're gonna do a few things that we thought just could be nicer For C sharp developers. So we did we fixed constructors and naming and different dotnet idioms And I like to call it C sharpier. It's a C sharpier API We had global state so before you can only kind of one windows director and that's kind of what we'll talk about here Is but you can actually have multiple screens going on so if you're in the desktop space actually have a multiple screens I mean actions immutable. We added new get in PC Lified everything pickled it if you will Added extensions and we like to call sanitizing the hierarchy. You know go over the hierarchy of a game But we did lots of other things in a cross-platform way like events and colors and labels to just simplify the API So anyone new coming in it would be a little bit easier just to kind of get on board So the Cocoa sharp API is actually quite simple and I'll just take like a few minutes before we jump into kind of building out a game To kind of go through the essential parts. You kind of know what I'm doing when I'm building out a game So the scene hierarchy you could think of anything on the screen is an active screen And there's a few steps that you have to do to get there So you have an application It's any application and that application as a director and that director what it's going to do and we'll step through this is Going to direct a few different elements of what's on the screen and usually it has one scene at a time So you usually have one application one director and one scene as you normally what you would expect in a game on a mobile device You're not having multiple windows or anything like that But since we kind of changed the API a little bit you could actually have multiple directors here now these scenes have multiple layers and each layer is consisting of Certain elements such as a sprite a label or a menu that might be on the screen and then All those layers can have multiple sprites So for instance you have multiple characters or multiple labels on a screen or multiple items in your menu So this is kind of the hierarchy of the game and what we're going to add on to is building out some sprites Which can be a flurry of different things like particle effects or just animations and you can interact with these different elements as well So the CC application so you'll see all over the place the the core Idiom that we put in place is CC so Cocoa sharp or Cocoa's so everything was Cocoa's so it's Cocoa's application Cocoa sprite so you'll see that over and over again in the actual Code that we'll be writing but the application itself is going to do the hard work for you Well, we wanted to ensure that you shouldn't have to worry about initializing Graphics drivers or things specific for the device or worry about open GL or whatever it's running on Because Cocoa sharp no one runs on iOS and Android, but it runs on Mac desktop It runs on Windows or runs on Windows phone Windows store. It's like wherever you want to put it It's everywhere you want to go but it handles as Application delegate and actually this is the core the beginning point of your game And the delegate here is similar as anyone done iOS development a few has anyone done Windows development It's like your app.cs. Basically. It's the start of your application so all your lifecycle events for your core application and This delegate it's similar to iOS UI application delegate, but it does a few things It enables us to point to say this is where my images are my sounds are at so Cocoa sharp can reference those later and then it also Sets the first scene, but it also gives us a lifecycle activity events So we can see when our application enter the foreground when it finished launching for the first time or when it goes into the background So back and forth so we get some lifecycle activity for our entire application and this is important if you're gonna Basically pause your game whenever your user maybe exits out of it or hits the home button or a phone call comes in You're gonna get this event and you're gonna pause things So a great thing that you probably want to do is pause the sound. That's probably one thing you want to do Or additionally if you're gonna save some state of your actual game, that's where you're gonna do it at is all in this application delegate Now the content folders I reference that the application is gonna have these content folders We're gonna put some things inside of there and we're gonna put some fonts sounds images You can even create complex animations with sprite sheets and also particle systems So if you have pen particle generating tools that put out like P lists Which kind of specify what is going on in the particle systems? You can put them right in there and we'll take a look at those as well and then Every application you just pointed a route and then you can say here's the search pass underneath it And you can even preload sounds ahead of time So for instance if you have an animation or the first screen of your application that you want to show and you want to launch immediately You can say preload this content ahead of time So your your assets load faster if you don't do that and you just want things to load on demand like you're loading a game level So then load all the content you'd want to handle that separately per level That's there, but if you have a simple game, maybe one or two screens you could load everything up front Maybe even just put a loading screen Then we have this thing called the director and this is kind of that first bit now We're broken away from the application now We're in the director and all it does is handle loading scenes So you can replace screens switch back and forth between screens and what we'll see here is you know You might have a launch page and then you need go in your main level you go to level two You go to level three and you keep directing if you will kind of like a movie producer You're just directing and what the scenes are going to take place and those scenes Are the various logic portions of your application and these all contain cc layers. So each layer of your application So really you're gonna handle mostly deal with a layer the scene is only used By that director to point where you want to go So that layer for instance is really important. This is where you're gonna do all of your business logic You're gonna handle your game loop your main game loop. That's always running We're gonna schedule Activities to occur so maybe I want to check collision every frame or every second Maybe I want to spawn a new enemy my you know detect when a user is moved into a certain region and then react to that And and what we're seeing here in this little bit of game logic is that we're creating a new cc scene with the main window Every application has a window We create this start layer that's there and then we add it to the game to you know to the scene So that scene could have multiple children layers on top of it that you could direct around And then those sprites so those layers are gonna contain sprites And there's multiple sprites that you can have you can have sprite sheets So we see the monkey on the bottom there. He's actually an animation sprite So he's actually wobbling back and forth kind of like this if you will little mono monkey so he's gonna be doing that and These are all gonna be inside that content folder and that's what's really important We've standardized on this content folder So you add your images on Android on iOS on Windows you can optimize the the The density if you will so even on iOS you could do at 2x at 3x so you can handle retina displays and same thing on Android You could actually handle HD images if you're going on a high resolution device for instance and Each of these Sprites that we're seeing here is just a cc sprite and we'll load them in different ways And what's interesting here is you have a cloud and you have like this ball here and this ball is also just a Sprite sheet, but it doesn't have any animations You could add everything into one big sprite sheet if you've done any game animation you kind of just you know snip it all out so that way You don't have multiple images that you have to maintain it's just one sprite sheet that you're gonna be loading up And there's tools that we interact with so now that you actually have things on the screen We want to interact with it in some way now of course you're gonna be running a game loop so you can actually have Elements falling or moving around or shooting a bullet that that bullet has its own like logic that's running But if you want to perform tasks on these sprites and we call them nodes So each node there you might want to tap on something or perform an action and actually be multiple things An action could be a sequence. Maybe it's a spawn sequence. Maybe it's a rotation Maybe it's an animation and you're running these actions in parallel You can run them asynchronously because we async a waitified everything so if you want to wait on one thing to occur Or we have a way of just streaming up a bunch of actions in a row So for instance, maybe you tap on the screen We want to move a monkey to that new coordinate You want him to kind of animate over there and then you want him to stop animating when he's done So it'd be three specific actions that you queue up in a row Which is pretty nice Now the touch events which we're actually going to interact with as well There's multiple events that we've given to you as developers to interact with Normally, it's just kind of one touch at a time. So if you're just tapping on the screen You can see when all touches are occurring you can Assign a touch to a specific node so say when that that monkey was tapped or when that banana was tapped and get a specific touch event for that And you get multiple events like when the touch begins so when I actually touch down If I move and you'll get coordinates across the way and then when I actually pick up off of the screen So we enable you to get all of those and what we're doing is we're handling all that business logic For you automatically of you having to manually register for the different events on iOS Android and of course windows And what's really great is we even enabled multi-touch on there as well So if you have a game then it requires multi-touch and your device supports multi-touch like almost all of them do now You can actually have multiple coordinates and see if people are touching the screen in multiple locations So if you're building out that type of game totally doable But there's a whole lot more that we're actually going to try to cover and kind of take a look at and you'll see a little bit of this In the actual game so we have a cross-platform accelerometer So you can actually see if the device is actually moving and rotating We have a cross-platform audio to play background play sound effects do a lot of things We have primitives. So if you don't want to do any Actual artwork in your game you can do a hundred percent is drawing lines and primitive graphics and the scales across all the different Platforms, so if you're gonna do like a geometry Wars clone for instance, you could totally do that With advanced particle systems so you can spawn particle systems and those are caked in we have a few for you automatically You can pull in your own particle systems and just spawn them off and and have them do whatever you want So for instance, you could just keep spawning a background image That's just doing crazy stuff and firing off or rotating over and over again We have parallax as you see as the monkey moves up and down the actual clouds are moving with it So you can say I would like that That as this monkey moves keep it to be a specific I'll go back and forth so you can see it again I want to parallax those actual clouds you kind of link those two objects together to move them around which is really cool It's handled all for you automatically We also have multiple effects so we're doing like wavy type things and spinning things Automatically and we'll see that we're actually gonna set up some some effects actually spin this banana and move this ball around and move it around on the screen and Finally, we have an a very advanced physics system In the actual game logic as well So this actual box that's kind of moving it'll actually handle the physics of gravity for you automatically kind of in a world space And all of these little samples that you're seeing here are all available Actually on our github repo because everything is open source We actually have one huge huge solution that has like every single sample in use case So it's like if you want to try to do something we have a test for it So that's kind of the core of cocoa sharp. There's a lot in it It's a lot of text kind of on a slide But what I wanted to do is just kind of build out a game and take a look So when you get started and I'm gonna do everything inside a visual studio You can do everything also inside of I mean I'm gonna do it in Xamarin studio You can also do everything inside a visual studio and I'm just gonna focus on iOS and Android But what you'll see in the actual business logic of the iOS and Android application as though there's three lines of code So if you want to then bring your application to a new platform all you have to do is copy the image assets in Add three lines of code and you have a game. It's just pretty awesome So we kind of start and I like to start everything actually on Github so this is the github and I'll zoom in here for the github. It's github.com Mono Cocoa sharp now we put this on the mono Mono directory because it's kind of built on mono game. It's open source and actively people can contribute to it Especially if they're on the mono project and that's kind of what we're actually doing You know Xamarin where the creators and the maintainers of the mono project Which is an open the original open source implementation a net and that's what we're actually leveraging here on iOS and Android to bring That over to create iOS and Android apps. I'm gonna pull this down since this doesn't fall There we go. Cool. So if you want to see what we're doing and have issues or how to pull requests or look at the wiki Everything is here So every single bit of the entire cocoa sharp library is actively here Including you could load this up on Android Mac OS. So we have Ooya support And then all different types of windows and windows 8 and phone support as well You can see what we're doing in here. We have a forum So if you want to actually go and actually maintain to it actually have questions We're active there and we have this wiki and everything is under MIT license, which is really really great On the wiki it kind of gives you everything how to get started More links to the forums so we have a slack channel And we even tell you if you want to deep-dive on why we created it what the core differences are and getting active With the new get packages and look at what we're doing in the future as we evolve cocoa sharp everything is going to be documented here Now what we did is we created some project templates So I like to go over here and you can see there's project templates in our wiki and The project templates all it does is tell you how to get started in other visual studio or Xamarin Studio In visual studio all you do is just kind of download a project templates of the vsix file Into the extensions and that kind of gets you good to go over there Now if you're inside of Xamarin Studio Xamarin Studio is our IDE. It's built on it works on both Windows and Mac It's a lovely lovely ID eats very similar We built it with the visual studio developer in mind So all the key bindings are the same across the board and it looks very much like an IDE We have built-in source code analysis in it if you ever use Xamarin Studio at all we have this thing called an add-in manager very much like visual studios add-in manager But when you go over to gallery, there's a cocoa sharp entry and all you do is install the project templates And that'll kind of get you up and running to do iOS Android and Mac development for cocoa sharp and that's really convenient So once you have that installed to get started with cocoa sharp you're gonna set new solution and under app we're gonna see Some brand new templates in here this cocoa sharp showcase and Empty project so we're just gonna kind of do file new project see what that looks like so we'll say And DC cocoa sharp there we go Now we distribute everything through new get so Everything is just a new get package and on up top here. It's gonna add a bunch of packages for me automatically Over and over and over again. We'll probably even update them. Let's take a look here That's good. Let's see what we're on 140 perfect This is actually pretty nice it's gonna okay got everything awesome So inside of here what we have is an Android project an iOS project and a common Lib and that common portable class library is gonna handle all of our game logic of our application And we can just see that there's just a cocoa sharp PCL inside of here So the main app delegate that's here. This is gonna handle kind of our business logic starting our application So we can see if the application finished loading and inside of here It sets up kind of some default state for us to say hey, I have a content folder. There may be some animations fonts sounds I really like this to be 1024 by 768, but it'll handle the scaling for you automatically on these different devices If you're an Android developer at all, you know, it's a crazy crazy beautiful disaster over there of a bajillion devices and we handle all that for you so scaling or putting borders around your game for you automatically and Then here we'll actually handle a high density or low density images And then it starts and creates a game layer as that child and it runs with that scene So that's the start and we can see we even have a little pause state inside of this that you could check at any time Then we have a game layer so this game layer is going to do a few things It's going to load up some sprites and if we look actually inside of this content directory Over an iOS. We'll see some images in here HD and Lodi. It's just a monkey. That's inside of here. There he is adorable So it's gonna load up a few monkeys add it to this layer. So this this is a layer a CC Great layer gradient that's in here So that every single one of your screens will be some sort of layer a solid color you can add a background or this one's gonna be a gradient We're gonna create some of these CC actions and the default there. We have some move by we're gonna create a sequence of Repeating it and we're gonna spawn this this this effect this wave effect So I'm just gonna kind of run this so we can kind of see what it looks like I'm gonna select an iPhone 5s simulator Let's see what this looks like. Oh Go ahead and spin up there So something too too crazy It just kind of shows you some things that you can do so loading some sprites putting them on the screen and kind of moving them around We're also generating a particle system in the background and what I can see is if I want to interact with this I can tap on the screen and that particle system moves So I want to kind of show off the core of what we're gonna be doing when we actually build out a game So up here what we'll see Or is it that down here? Run with there it is We have this event so we create an event which is a listener to say whenever anyone touches anywhere on the screen I could subscribe to multiple touch events So there's on began on cancelled on ended on moved and whatever I lift up here We go in and we run this touches ends and what we can see that all I'm doing is this galaxy system That's creating all these particles. I grabbed the first touch event grab the location if it was moving I stop it and I say let's move it somewhere else on the screen And then I told to move over a certain amount of time so over this duration move it to this position So really really simple and I run this action. So everything's action based inside of here. It is really nice So that's what is going on here. So if I move this around we can see it just kind of moving around on the screen Now if I load this up over on Android we can take a look over here. So let me go and pump up an Android simulator real quick Now I'm going to be using our Xamarin Android player, which is a free Emulator from Xamarin anyone can use it for any type of development on Mac or PC It's all based on virtual box. I was really really fast Which is really really nice. Let's see if this will spin up here. So we'll go ahead and pop it over here Just make sure I have everything set up correctly in my build actions. Nope No got an email perfect like that. There we go And What we see is actually in the iOS or Android application We have our our actual iOS app delegate and this is kind of the start of the application So I'm creating that shared app delegate and then over in I got to rotate it. There we go Then we do the same thing on Android, but the same exact game same image assets everything like that now over on Android I didn't actually write any Android code. I just everything in C sharp Because I'm using Cocoa sharp So if I look at Android over here, we can see that I'm using some packages Cocoa sharp Android in my main activity What do you know? I just have a little bit of configuration and I start the game That's it. So even if you don't know Android or iOS development at all You don't really need to because you're just learning the Cocoa sharp API and it's just going to run over there now if you want to tie in for instance into like Google Play Games or the the iOS equivalent for what it's called game center game center. That sounds right You can actually add that integration if you want to put on Amazon devices You could download the Amazon SDK and put it right in there and the same thing would be true over on Windows You would just pull in the Cocoa sharp Windows new get package add these same lines of code And then you could integrate with anything you want if you want to integrate in Cortana or something like that You absolutely could because you could just bring in those packages So let's actually open up another game and actually build some things out from scratch and walk through kind of building out a game and The kind of sample you saw with that little little monkey kind of moving around and collecting bananas That's a game that we have called gone bananas. Go figure. We're big fans of monkeys as you can see this is Sebastian He's up here hanging out with us today and and this gone bananas is available for every single platform Windows iOS and Android and I've just opened up the iOS and Android bit here and what we're gonna notice is that we still have an Assets folder and we have content inside of here. I added some custom fonts into my application Some sounds I have an mp3 and I have some wave files And then I have some I've an animation so if I open up this monkey here. This is that same thing We've even stuck the banana in there so we can pull it out just a sprite sheet And then I have this p a p-list which was generated by a tool for me if I open this up think Here let's try to open it There we go, it's just keyframes so it says what frame do you want to use per so it's just like a Spits that out for us and then we have the balls There's the balls that we're gonna be dropping from the sky and the idea is that these bananas and balls are gonna fall from the sky We want to collect the bananas if the if the actual balls hit us then the game's over But we also have this background that we're gonna use grass and that'll be kind of nice all right perfect So that's everything that's inside of my core application if we look at the app delegate again on iOS and Android is gonna be the same Except we're gonna create this gone bananas app delegate And that's what's gonna start here, and I want to write a few hundred lines from scratch So some of this code is in place some of it we're gonna write out, but it's gonna look very much the same We can see that I'm preloading some effects sound slash taps sound slash background music I'm just grabbing the main window size and setting that as the default and I'm running the game and we're running this start layer So there's three Three screens to our game. There's a start screen a main game loop that's gonna run all of our business logic and an end game And we can loop those back and forth so the start of our game here If I open up the main game layer It's pretty pretty simple I have a click handler and all it does is whenever I touch anywhere on the screen it'll start the game It's gonna start my main game layer, and I just add that touch listener I have a black background and all I do is put a label we create a CC label and you can by default Just slap down some text load up custom Fonts and we can see I have aerial in here because I brought an aerial you set Specific size and then you say where you want to do it. I'm just gonna align it in the center and add that in That's it The the main game layer that we're gonna interact with here is gonna do a bunch of stuff and we're gonna kind of walk through this later But when this is over what we're gonna have is a game over layer and these two are kind of already caked and all this one Does is it looks very similar to our first game, but it takes in a score so I could display a score on the screen on there and I have a score label and a play again label whenever they tap it It just brings them back to the previous screen to start a new game. So very very simple inside of here That's it. All right So let's go into our main game layer and that's where all of our actions gonna be and I set up all my variables ahead of time So how fast do I want the monkey to move? How long does my game last which is gonna last 60 seconds and how many maximum amount of balls do I want on the screen? We have some physics. I'm not gonna go into physics logic. So I'm not a physics major I just kind of put that in a helper class, but we have a bunch of physics that's gonna happen And then we can see what else I'm doing here So first I'm gonna set up what I want to track in my game. So the elapsed time so after 60 seconds I should end my game that's gonna be a goal. I'm a main monkey and I'm gonna interact with so it's gonna be a main Sprite, he's just gonna move around and animate on the screen And then what I'm gonna do is I'm gonna keep a list of visible bananas and hit bananas So the idea here is that I can just kind of track and actually see the rotation and check the collision of any visible Bananas on the screen and then the hit bananas will go into a list that I could track later that I've removed from the scene and I can Get a count for them later on And then I'm gonna kind of start setting up my game and some of these are just Animations that'll be played and we'll set up so we have a walk animation and we'll set that up in a bit a walk repeat So it'll loop forever Animation and a way of stopping it. So this just says hey on any node stop all actions That's a little extension method so you have a way of saying if you're moving stop right now Just stop walking stop doing whatever you're doing. I Also have a sprite just like we had our monkey. I have a grass. That's gonna be our background sprite. I Have a rotation so I say over point eight seconds rotate the banana 360 degrees we handle all that logic for you don't have to worry about it We'll take your sprite and rotate it just with this action and Then what we do is we I set up a little action just like we said stop all actions when I want to stop walking Also remove this banana when it's done. So I'll remove the node that banana node from the parent So I'll just remove it from the game scene if I collide with it And that'll be any any node and then I set up my balls that are here in my physical world So I have a little bit of logic just initializing And we're gonna walk through adding some things side by side But I just wanted to do a little bit of setup here So I might a touch listener touch all at once and that's whenever I end and pick it up I set my background color to white initialize my list and in this instance I'm gonna create that sprite batch so those balls were in a batch file basically inside of there I'm just gonna pull down that texture and split it all up. So I have my balls ready And I'm gonna add grass add monkey and start scheduling these are all blanks We want to fill those in Now all we need to do at this point is actually start adding some things to the screen So let's go ahead and do a few things It's out here. Let's remove our monkey so we can kind of add one thing at a time so we're gonna add our grass and Our grass is gonna equal a new cc sprite And it's gonna take in a file name. That's gonna be grass There we go I should say let me just run this first. You can see what it looks like. I think this should run Let's take a look So I've really done anything just initialization and hopefully if I didn't crash anything it shouldn't do anything Perfect there's Tap screen to go bananas and it's white. So that's what we have that's our game right now We're gonna transform this over the next 20 minutes So we're gonna add some grass So first we'll do is we'll initialize that grass and then we'll say add child to this layer and there's grass now I do get some events. I get my application event lifecycle But each layer also has some event life cycles as overrides And we're gonna see whenever this was added to the scene whenever this this layer was added to the scene I can initialize a few things So I'm gonna go ahead and position my grass right in the middle so we can run this again So I create my grass add my child and run it again Grass that just happened two lines of code not bad. Let's put our let's put our monkey down on the screen So here what I'm gonna do is this is a little bit more logic So I didn't want to write this from scratch, but I'll walk through it with you So instead of creating a sprite this monkey is a sprite sheet with all those animations So what I have to do I'm gonna pull this down a little bit. We have a little inline function here Let's say load up that animation Peelist info that sprite sheet that's gonna have the different keyframes and Grab all the animations that start with frame because frame one frame two frame three frame four for each animation And then I'm gonna initialize a few things I'm gonna animate and create an animation So I take all those keyframes and I say each frame should last point one second. So we kind of moves across the screen That's the main animation and then I'm gonna create a repeat So just keep animating that walk animation over and over again So there's an animation and I tell the action is to animate the animation and repeat that forever So just kind of always keep doing that and the last thing that we're gonna do is create my monkey Based on just the very first sprite in that list give it a monkey name and And then I scale it point two five percent. It's a really big monkey and then I add that monkey to the list That's it. So we have our animation that's gonna be looping over and over again, and I create my monkey So now I can also put the monkey right in the middle load it up again Now we got a monkey But now of course we want to do something with the monkey because if I tap on the screen I'm actively tapping nothing's happening So let's move them around a little bit So we saw earlier if we look up top I created this on touches end Here it is. It's blank. I have it down there in case of emergency. We're gonna try to type this out So every time I move we want to move that monkey to a new location and we want to animate him So the first thing that we're gonna do is I'm gonna say a monkey Let's stop all of your actions. So if you're moving if you're doing anything if you're rotating for some reason stop doing that and calm down Then we're gonna grab our location and that's gonna see we have a touch event So you can see which touch event came in but also a list of CC touches So we'll grab our first touches out of that list and we'll grab the location on screen so the physical location on screen and Then we what we need to do here is we need to take that location And we're gonna call a method called world to screen space And what that's gonna do is it's kind of take the current location on the screen and put it into our world that we have here So I can manipulate it Now we have to do a few things We don't want all of a sudden to say move that monkey over there in point one second and he goes racing across really fast Or maybe he's only moving just like a few pixels So we need to calculate how fast this monkey should move. So what we'll do is we'll create The distance we'll calculate the distance here. So let's say distance And we have some helper methods Inside of here on our point. So there's a point x and y coordinate. There's a distance We can calculate the distance between two points. That's pretty pretty lucky So we'll say monkey position where you're at right now to our location and we're good to go And then we'll say speed There and we'll take our distance and we're gonna divide that by our monkey speed So it's gonna kind of normalize our speed over distance ratio I may not be the best math in the world, but it works for the sample Now we need to actually go ahead and actually Move our monkey. So we have some moves in here. We can move by move by state move to move to a state So we're gonna move to over we'll do it over that Speed all right. Sorry. This is calculating the time. That's my fault. Sorry time The distance divided by speed that equals time. Yeah, I think calc math. It's been a long time So one thing I forgot about game development when I was starting all this like oh math And we're gonna move that to the new location there we go And we're gonna tell our monkey a few things we're gonna tell our monkey to start animating We're gonna tell it to move and then at the end of it. We're gonna tell it to stop animating So we'll first say run action monkey. Oops. That's not it say move to there we go monkey dot start Where'd it goes run action? There we go run action. There's run action async run multiple actions And we're gonna tell it to walk repeat Then we'll say monkey dot run actions So we're gonna run two in a row so this will pair them up so it'll always walk repeatedly And then we're gonna run some other actions So we're gonna say move to first and then we have a walk animation stop So we're kind of chaining up multiple events. We're gonna say hey run this first animation and keep running it That's our animation that's looping over and over and over again for him walking and animating and then run the action to move to that location And then what this does is when we're done it'll run the next action Automatically, but we're running them in two separate calls. So it's always walking and repeating over and over again So let's run that again There's our monkey I'm gonna tap up top boom if I zoom in you can see I'm moving around pretty adorable pretty adorable alright And that's your game done. I'm just kidding. Alright, so what do we need to do next? So the next thing that we need to do is actually schedule some things So we've added our grass added a monkey and then we need to schedule some things and what does that mean? Well every game that you ever create has a main game loop the game loop is always running every single frame and you need to update it So what we did is we enabled a way of Scheduling things and you can schedule it's kind of like a task-out run almost What we said is keep running this task at certain intervals in time and you can schedule up a must a bunch of actions over and over and over again So if I come down here After this we have this start scheduling and I kind of lined up what I need to do So the first thing I need to do here and I start scheduling is I need to schedule a banana I need to schedule to add a banana to the visible bananas list I'm gonna add some time to our game loop. So I'm tracking up to that 60 seconds Over and over again. I'm gonna check to see if the game is over and then go from there So down here. I created a few helper methods. We have is time up pretty simple math is the elapsed time greater than the game duration And then I also had this add banana thing and what this is gonna do It's gonna load up our sprite sheet take out the banana That's in there. It's gonna use a helper method to start in a random position So I have a bunch of helper methods that I'm not gonna go into it's a bunch of more math and logic and shenanigans So we'll just pretend that I went over that. It's all the other stuff's online We're gonna grab a random position We're gonna start and here's the banana on the screen at it and then similarly like how we're removing the Actual monkey on the screen. We're gonna do the same thing. We're gonna move the banana down to the bottom of the screen and Then when it's moved to the bottom of the screen remove it from the view and then always rotate that banana over and over again So you're gonna see it spinning on the screen So let's just do that. We'll come up to our schedule and we have a keyword called schedule Now don't be worried. We're gonna actually put some inline actions inside of here. We're gonna use this one It's gonna take a float which is the time that it was took in between the last call and an interval So we'll say t and we'll just put a little stuff in here and we'll do auto 1.0 f So every second we want to add a new banana to the game. So we'll say Visible bananas dot add we're gonna call that add banana there we go We're gonna say our elapsed time plus equals t That's a time difference between there. So one second two second three second But it's it's always one second so we could change this and this could be variable then what we're gonna do is say if is Time up Let's end game Now this end game method is down here and I haven't done anything We're gonna do that later But our core business logic is in place and we can start it up again and just see what the scheduler is gonna do over and over and over again And for lucky a whole bunch of bananas should fall from the sky Boop And that's pretty cool so far and just doing it all randomly so I'm kind of getting a random x in there. That's pretty cool Alright, so let's do another thing. Let's go ahead and schedule another one so we'll say t and we'll come in and we'll add a ball now In here, we'll do that every I'm gonna do every point one five seconds because it's pretty hard with point one and I have a add ball method and That of course is gonna be over in my little helpers class and what this is gonna do is it's only gonna add the ball if If we have don't have our maximum amount of balls in there So the balls are already is gonna kind of be there. It does a whole bunch of 2d vector stuff to kind of like bounce around so it bounces up and down off the physical world space That's created more math. Don't worry about it Games don't worry about math. It's not important. So we'll run that again. It's actually very important learn learn lots of math Cool so we'll tap again and now what we'll see is now we have a ball But the balls are just kind of the top of the screen because we haven't animated it at all yet. So we need to do that But they're there. They're they're occurring. That's interesting Boom. All right. We're almost there. All right, we could do a few things. We could schedule Every second to do something. We'll say hey just every or every frame. It could take in just an action We'll say check collision inside of here now check collision Not to do T Just do this to make it easier on myself. There we go now check collision There we go is another helper method that I wrote but it's actually really not too difficult What we have inside of here is bounding box transformations now Bounding box might not be the best because you know, it's a monkey with a banana But you could actually we have actual documentation of how to actually put multiple bounding boxes around that monkey So you might want to do the banana than the actual body inside of there But for simplicity's sake I'm saying for each visible banana in the list If the bounding box that's transformed around it enters interacts with the monkey then that banana is hit We're gonna play a sound effect called sound slash tap. We're gonna explode that banana We actually do a particle and we'll look at what that looks like and we're gonna remove it from the parent And this explode thing here is Is actually a particle explosion. We have a bunch of them caked in automatically But this one just says create and do an explosion of particles create 10 of them and automatically remove it from the scene when you're done And that's it But really just doing collision just looking at the core collision that you have like that's pretty simple math Just every frame loop through my visible bananas and be good to go now, of course kind of best practice If you have a thousand bananas on the screen, that's gonna be pretty difficult to check every frame So you might want to actually do some more logic inside of there with like link or something because links super fast, right? so So we'll check collision and we'll still see that these balls are up top that's that's cool But we should this gonna like make the game really really great Because we're only just checking the bananas. I'm just gonna win forever So let's see here. So now if I actually tap here, boop It makes the game easy to win Perfect so now we got some collision going on we got some particles happening, which is a few lines of code Now we need to schedule a few other things this one's a little bit more difficult Because what we're gonna do is we're gonna loop through all of these balls on the screen We're gonna do the same kind of detection to see if I actually am inside the physics body of the specific ball And if so, I'm gonna remove that sprite and then we're gonna actually move around on the screen that's a little bit more complicated But this is gonna move the ball and destroy the balls when they're not moving So it's a little bit more math inside of there. So let's take a look here So now our balls are falling And that's bad. Let's see if I put any collision not no collision yet. Cool Perfect So now what we need to do is actually Do a few more things which is end our game. So we have our balls We have our things occurring inside the game and then we can actually end our game if we hit a hit a physical But or a ball on the screen so unschedule everything unschedule all We're gonna create a new layer. We'll call the our game over Scene with score. We're gonna pass in a main window There's our window and we'll take in our score and that's gonna be our Bananas no, it's called hit bananas dot count. So how many we hit and then what we can do is say What do we have to do it's the director thing Sometimes I just forget core logic window. Default director there we go window dot default director dot scene Replace scenes or it's gonna move and we're gonna move to this new layer or scene if you will and that's actually should be seen There we go. Perfect. And there we go So that should be good to go now I need to double check to see why my check collision is here Hit bananas ball count good to go balls hit count end game great. All right. Ah So check collision it does a little bit more than that it adds in it Hits updates the hit and then it also does some detection here on the the bounding box of the actual Balls that are on the scene. So now we can move this again Now Ideally I should be able to kind of get some here and boom I hit the ball that's interacted with it and I got two bananas if I tap again. I start up a new game And we're kind of good to go now. It's kind of boring because there's no background music That'll really spice this up. I'm pretty sure so let's go ahead and add that So I'm gonna come up to the top Way up top here into my game layer. I'm in the right scene. There we go And after we set and start scheduling we're gonna use this thing called cc simple audio engine We're gonna use the shared audio engine. So anything that's kind of all the sound effects and our game music That's there. We can adjust the volume. We can also play background music And if we take a look inside of here, what we'll see in our content sounds We have background music. So we'll say sound slash background Music mp3 and we'll loop that why not Boom thousand times better basically best game ever. It's pretty cool. Not even moving I'll see how you just gonna dodge all the balls. That's a pretty awesome monkey right now. I'm just like, oh, there we go Pretty good music, I think we've all that for like a dollar All right, so what's really cool now is that I can now just set Android as my startup project here and Actually select that same Android device the emulator that I had there. Let's rotate it back. This is a portrait game The same exact game same particle physics everything is reused a hundred percent across the board and this is scaled down Obviously so I sick ale this up, but it's a full another game on Android No additional work that I needed to do because everything is in my core Portable class library with Cocoa sharp, which is pretty mind-blowing to me overall And you kind of have a full game with just a few lines of code some math and some physics But besides that it's all there. We can all figure out physics. It's easy, right? well Now all of this source code and more is all available online and I'll have these slides on my slide share But you can just always tweet at me, but it's also all on that Cocoa. So when you go to the mono slash Cocoa's Sharp repo on the mono GitHub. There's also the sharp Cocoa sharp samples and there's tons of games in there Not only just this one, but there's an extension to gone bananas that actually has Apple watch support So you can control the monkey from the watch, which is really crazy Which is really cool. It's actually published. It's called gone bananas. It's on iOS and Android by one of our evangelists a previous evangelist Mike Bluestine because he created this sample, which is awesome a whole huge walkthrough of creating the step-by-step I didn't even go into the parallax or creating the Sun with the spirals and the background everything like that There's a lot of cool additional stuff that you can create. I'll let everyone take a second there to take these snapshots I'll tweet these out later as well. I'll have the slides online If you follow me on Twitter Now a lot of people ask about pricing like what's free. What's not free? Well Cocoa sharp is open source And if you want to create Windows applications, you don't pay us for that You pay Microsoft for whatever you want to do if you want to create games with Xamarin and Cocoa sharp Does cost some money some subscription to do that We have an indie edition starting at $25 a month for iOS and Android to publish your game But to do it on Windows or anywhere over there, you're good to go once you have your Android or iOS application You're paying us for the compiling the technology to bring that over and build the iOS and Android app We haven't gotten sorry with Xamarin just playing around with it. You go to Xamarin.com. We have a full 30-day trial I did mention Xamarin University earlier. We have tons of great stuff. I have some Xamarin stickers and plural site stuff up here There's tons of great resources including our documentation to get started, but we just yesterday I'm super excited that we did it yesterday and not tomorrow But we have a full 30-day trial of Xamarin University Which is live interactive and structures that go online and a go-to meeting and you learn 24 hours a day seven days a week And it's like you're like a student you become a fully Xamarin certified developer The free trial gives you a few courses and access all of our guest lecturers I was just listening to Miguel decaza our CTO. I one of our guest lecturers talk about just the state of net is really intriguing I did that before I went to bed Which is really cool So I recommend everyone checking out Xamarin.com University because it's free to do a free trial and there's no credit card or anything that you need Of course, you can find me at any time just at James at Xamarin.com and at James Monta Magno nice short and sweet Twitter handle And I have a Xamarin form session in this room at 1620 the very last session the day. Yeah, so with that Thank you guys so much I'll be around as well up here if you guys have more questions. I have some plural site stuff and stickers as well
 Awesome look at that fancy transition. Whoo. That was awesome. So how many of your C sharp developers?  Awesome, you're all game developers that just happened. I just turned you all into game developers  How many of you guys have ever made games at all like 2d3d?  Quite a few how about like for councils a  Few all right you guys win  So a long time ago  2006 I lived in Phoenix, Arizona  And I wrote a lot of C++ code and I made a game for the Xbox 360  I had it dive deep into the Facebook archives actually find it and a little video. It's an FLV  Whenever that was made so here's my history of video games  So I did this with super sweet music  So that took about two years and I did that for a small company and then I said okay  I'm never gonna do that again  Like that was the worst experience in my life who wants to write C++ code over and over and over again  So I learned C sharp and then I wrote printer software for four years games to printer software natural transition  It's fantastic. I really like to say that you lead need at least four years of  Enterprise printer software under your belt to put software engineer on your LinkedIn page  It's just a given like you have to have it. So what happened? Well, and who am I? Well, that's me. I'm James Montemagno  I'm a developer evangelist at Xamarin  I've been creating and crafting dotnet applications for the last decade after ever since I moved out of the game industry  And I live in this wonderful place. It's just like Oslo, but it's called Seattle. It's lovely. It rains there, too  We got that it's cloudy, but it's also beautiful  I'm blown away when I got off the plane my girlfriend says how's Oslo?  I said it's just like home and it's actually really true great coffee great people just actually really really beautiful  But I moved there four years ago because I said enough is enough with printer software  Because really you can only take about four years of printer software any software developers writing printer software right now  Perfect you all you all nailed it never do that never do that  It's actually it's not that bad you learn great enterprise like when you know are porting a legacy dotnet  11 application to 2.0 to 3.5. You're like, that's great  So I said enough is enough. I'm gonna move to Seattle not only because we have the most amazing coffee everywhere besides Oslo  Maybe I'm going to Tim's tomorrow. I'm very excited about that like I came to Oslo  I was like, what do I need to do coffee like oh go to Tim's very excited  But I moved there because I love mobile development. That's been my main focus  I went to the final PDC before there was fancy builds and everything like that got my hands on a Windows phone  I was like, oh my goodness there's so much power in the palm of my hand and I found Xamarin because I was a C sharp developer  And I've been crafting iOS and Android and Windows applications for the last four years and every single thing that I do  Is open source complete on my github?  I can find me anytime on github on my blog at James as Xamarin.com or really just on Twitter  That's the easiest way to get a hold of me. I'm up probably 24 hours a day probably on a plane somewhere flying somewhere  I like to say I code 50% of the time on a  Plane 50% of the time presenting 50% of the time and helping all of you guys 50% of the time  Create create awesome mobile applications. So I'm kind of 200% always on so we're here to talk about  Cocoa sharp and building 2d games all in C sharp  I'm gonna tell you a little bit about Xamarin. How many people have no idea what Xamarin is a  Few per is always like one or two so Xamarin  I have a talk later on Xamarin itself with Xamarin forms building like native  Applications for iOS Android of course windows all in C sharp  And the Xamarin platform is kind of actually what powers Cocoa sharp in a way  It's enabling us as C sharp and dotnet developers to get away and still create windows applications  But come over to iOS Android and Mac and use all of our dotnet and C sharp skills and libraries to create great native  Applications that's a Xamarin platform. You can do that in visual studio or Xamarin studio RIDE  We also have a great testing product called Xamarin test cloud to do automated user interface testing for any of your iOS or Android apps  Across thousands of physical devices. We even have a crash of litics and analytics software for your mobile applications and  Entire training online university called Xamarin University go figure  It has a free trial actually so you can actually go take a few classes for free and learn all about Xamarin and creating iOS and Android apps  And how this all works and kind of even how the Cocoa sharp bits works is the same way that Xamarin works  We need to build out a shared C sharp back end for any of your mobile applications  And these are all your business logic of your application. So your models your view models  Your SQLite databases your calls to Azure to AWS or wherever you want to go any of your dotnet code your things that your  Applications are doing the same on each platform  Then we give you the tools or frameworks to build on native user interfaces for iOS Android and Windows now  I usually talk about creating just mobile applications themselves either for the enterprise or consumer  But you can think about this iOS and Android and Windows  UI up top there as just a game UI and that's where the Cocoa sharp libraries gonna come in and replace that  Native kind of storyboard or Android XML with actual 2d game engines and user interface  So that's what we're gonna talk about today  And there's actually several ways of building games with Xamarin not only just Cocoa sharp  So if you want to tie into the actual native platforms or in 3d as well, there's ways of doing that  I like Cocoa sharp  Because I never thought I'd go back to games and then I went to a game jam and created a game in like 12 hours  And I was blown away and I'll show you guys that later on  But we have mono game and that's actually the core of actual  Development it's a it's a it's a port of XNA over to iOS Android Mac and even Linux that powers tons of amazing games like  Bastion and Towerfall and a bunch of amazing games out even on like PlayStation like transistor for instance  So that's all in C sharp. We also have a mono for unreal, which is a project that we've been working on  It's just for desktop at this point in time  But if you're kind of in the unreal engine type of space, there's mono for for unreal so you can write everything in C sharp  Which is very cool  Built into core iOS is a few API's that Apple gave to actually create  Native 2d games and sprite kit is one of those if you use Xamarin to actually create your native iOS and Android applications  You get access to all the API  So if you could do it in objective C or in Java and you can do it in C sharp  So we expose sprite kit and also scene kit as a way to build 2d and 3d games just for iOS  But I think Cocoa sharp is kind of like a sweet spot in between because it blends the paradigms of what we kind of want to do in 2d games  with C sharp idioms and  Miguel kind of wrapped it up like when people ask what is Cocoa sharps?  And he says it blends the power of the Cocoa's 2d  Programming model with C sharp and the dot net framework. So leveraging C sharp and dot net  And then what we did is we took what we kind of loved about Cocoa's 2d  And we added the C sharp and dot net idioms on top of it  So if you ask Miguel why why did we create C sharp or Cocoa sharp as part of Xamarin?  Well, this is why because we love the ability to kind of create 2d games with Cocoa's 2d  But we wanted to do it in C sharp in a way that dot net developers would kind of know and love with events and and all the great  Things about dot net in the back end. So how many people are familiar with Cocoa's 2d at all? Nobody, okay, cool  So well, let me tell you a little bit about the history of where Cocoa's sharp came from  Well, Cocoa's 2d is a 2d game engine and it was built in object for dejective C developers  So they create Mac and iOS applications  And then that got ported into the cross platform space with a Cocoa's 2dx  Which was all C++ based?  Well, that's that's great. Everyone loves to manage memory  So then someone said there's got to be a better way  So someone then went out and leveraged monogame  Did already be out and created Cocoa's 2dx na which was all in C sharp  But it leveraged new platforms across iOS and Android  So the first two there the first one only targeted iOS and then Cocoa's 2dx targeted Android and iOS and even  Mac desktop and then Cocoa's 2dx na which has been out for quite a long time  Targeted all the platforms  Now what we did with Cocoa's sharp was that we took what was there in Cocoa's 2dx na  We kind of loved what was going on there. Well, we wanted to contribute actively to it  So we kind of forked it and there's some logistics of putting things back into a different licensing  So we said, okay, we're just gonna open source all of Cocoa's sharp  So it's available completely on github  We're gonna do a few things that we thought just could be nicer  For C sharp developers. So we did we fixed constructors and naming and different dotnet idioms  And I like to call it C sharpier. It's a C sharpier API  We had global state so before you can only kind of one windows director and that's kind of what we'll talk about here  Is but you can actually have multiple screens going on so if you're in the desktop space actually have a multiple screens  I mean actions immutable. We added new get in PC Lified everything pickled it if you will  Added extensions and we like to call sanitizing the hierarchy. You know go over the hierarchy of a game  But we did lots of other things in a cross-platform way like events and colors and labels to just simplify the API  So anyone new coming in it would be a little bit easier just to kind of get on board  So the Cocoa sharp API is actually quite simple and I'll just take like a few minutes before we jump into kind of building out a game  To kind of go through the essential parts. You kind of know what I'm doing when I'm building out a game  So the scene hierarchy you could think of anything on the screen is an active screen  And there's a few steps that you have to do to get there  So you have an application  It's any application and that application as a director and that director what it's going to do and we'll step through this is  Going to direct a few different elements of what's on the screen and usually it has one scene at a time  So you usually have one application one director and one scene as you normally what you would expect in a game on a mobile device  You're not having multiple windows or anything like that  But since we kind of changed the API a little bit you could actually have multiple directors here now these scenes have multiple layers and  each layer is consisting of  Certain elements such as a sprite a label or a menu that might be on the screen and then  All those layers can have multiple sprites  So for instance you have multiple characters or multiple labels on a screen or multiple items in your menu  So this is kind of the hierarchy of the game and what we're going to add on to is building out some sprites  Which can be a flurry of different things like particle effects or just animations and you can interact with these different elements as well  So the CC application so you'll see all over the place the the core  Idiom that we put in place is CC so Cocoa sharp or Cocoa's so everything was Cocoa's so it's Cocoa's application  Cocoa sprite so you'll see that over and over again in the actual  Code that we'll be writing but the application itself is going to do the hard work for you  Well, we wanted to ensure that you shouldn't have to worry about initializing  Graphics drivers or things specific for the device or worry about open GL or whatever it's running on  Because Cocoa sharp no one runs on iOS and Android, but it runs on Mac desktop  It runs on Windows or runs on Windows phone Windows store. It's like wherever you want to put it  It's everywhere you want to go  but it handles as  Application delegate and actually this is the core the beginning point of your game  And the delegate here is similar as anyone done iOS development a few has anyone done Windows development  It's like your app.cs. Basically. It's the start of your application  so all your lifecycle events for your core application and  This delegate it's similar to iOS UI application delegate, but it does a few things  It enables us to point to say this is where my images are my sounds are at so Cocoa sharp can reference those later  and then it also  Sets the first scene, but it also gives us a lifecycle activity events  So we can see when our application enter the foreground when it finished launching for the first time or when it goes into the background  So back and forth so we get some lifecycle activity for our entire application and this is important if you're gonna  Basically pause your game whenever your user maybe exits out of it or hits the home button or a phone call comes in  You're gonna get this event and you're gonna pause things  So a great thing that you probably want to do is pause the sound. That's probably one thing you want to do  Or additionally if you're gonna save some state of your actual game, that's where you're gonna do it at is all in this application delegate  Now the content folders I reference that the application is gonna have these content folders  We're gonna put some things inside of there and we're gonna put some fonts sounds images  You can even create complex animations with sprite sheets and also particle systems  So if you have pen particle generating tools that put out like P lists  Which kind of specify what is going on in the particle systems?  You can put them right in there and we'll take a look at those as well and then  Every application you just pointed a route and then you can say here's the search pass underneath it  And you can even preload sounds ahead of time  So for instance if you have an animation or the first screen of your application that you want to show and you want to launch immediately  You can say preload this content ahead of time  So your your assets load faster if you don't do that and you just want things to load on demand like you're loading a game level  So then load all the content you'd want to handle that separately per level  That's there, but if you have a simple game, maybe one or two screens you could load everything up front  Maybe even just put a loading screen  Then we have this thing called the director and this is kind of that first bit now  We're broken away from the application now  We're in the director and all it does is handle loading scenes  So you can replace screens switch back and forth between screens and what we'll see here is you know  You might have a launch page and then you need go in your main level you go to level two  You go to level three and you keep directing if you will kind of like a movie producer  You're just directing and what the scenes are going to take place and  those scenes  Are the various logic portions of your application and these all contain cc layers. So each layer of your application  So really you're gonna handle mostly deal with a layer the scene is only  used  By that director to point where you want to go  So that layer for instance is really important. This is where you're gonna do all of your business logic  You're gonna handle your game loop your main game loop. That's always running  We're gonna schedule  Activities to occur so maybe I want to check collision every frame or every second  Maybe I want to spawn a new enemy my you know detect when a user is moved into a certain region and then react to that  And and what we're seeing here in this little bit of game logic is that we're creating a new cc scene with the main window  Every application has a window  We create this start layer that's there and then we add it to the game to you know to the scene  So that scene could have multiple children layers on top of it that you could direct around  And then those sprites so those layers are gonna contain sprites  And there's multiple sprites that you can have you can have sprite sheets  So we see the monkey on the bottom there. He's actually an animation sprite  So he's actually wobbling back and forth kind of like this if you will little mono monkey  so he's gonna be doing that and  These are all gonna be inside that content folder and that's what's really important  We've standardized on this content folder  So you add your images on Android on iOS on Windows you can optimize the the  The density if you will so even on iOS you could do at 2x at 3x so you can handle retina displays and same thing on Android  You could actually handle HD images if you're going on a high resolution  device for instance and  Each of these  Sprites that we're seeing here is just a cc sprite and we'll load them in different ways  And what's interesting here is you have a cloud and you have like this ball here and this ball is also just a  Sprite sheet, but it doesn't have any animations  You could add everything into one big sprite sheet if you've done any game animation you kind of just you know snip it all out so that way  You don't have multiple images that you have to maintain it's just one sprite sheet that you're gonna be loading up  And there's tools that we interact with so now that you actually have things on the screen  We want to interact with it in some way now of course you're gonna be running a game loop so you can actually have  Elements falling or moving around or shooting a bullet that that bullet has its own like logic that's running  But if you want to perform tasks on these sprites and we call them nodes  So each node there you might want to tap on something or perform an action and actually be multiple things  An action could be a sequence. Maybe it's a spawn sequence. Maybe it's a rotation  Maybe it's an animation and you're running these actions in parallel  You can run them asynchronously because we async a waitified everything so if you want to wait on one thing to occur  Or we have a way of just streaming up a bunch of actions in a row  So for instance, maybe you tap on the screen  We want to move a monkey to that new coordinate  You want him to kind of animate over there and then you want him to stop animating when he's done  So it'd be three specific actions that you queue up in a row  Which is pretty nice  Now the touch events which we're actually going to interact with as well  There's multiple events that we've given to you as developers to interact with  Normally, it's just kind of one touch at a time. So if you're just tapping on the screen  You can see when all touches are occurring you can  Assign a touch to a specific node so say when that that monkey was tapped or when that banana was tapped and get a specific touch event for that  And you get multiple events like when the touch begins so when I actually touch down  If I move and you'll get coordinates across the way and then when I actually pick up off of the screen  So we enable you to get all of those and what we're doing is we're handling all that business logic  For you automatically of you having to manually register for the different events on iOS Android and of course windows  And what's really great is we even enabled multi-touch on there as well  So if you have a game then it requires multi-touch and your device supports multi-touch like almost all of them do now  You can actually have multiple coordinates and see if people are touching the screen in multiple locations  So if you're building out that type of game totally doable  But there's a whole lot more that we're actually going to try to cover and kind of take a look at and you'll see a little bit of this  In the actual game so we have a cross-platform accelerometer  So you can actually see if the device is actually moving and rotating  We have a cross-platform audio to play background play sound effects do a lot of things  We have primitives. So if you don't want to do any  Actual artwork in your game you can do a hundred percent is drawing lines and primitive graphics and the scales across all the different  Platforms, so if you're gonna do like a geometry Wars clone for instance, you could totally do that  With advanced particle systems so you can spawn particle systems and those are caked in we have a few for you automatically  You can pull in your own particle systems and just spawn them off and and have them do whatever you want  So for instance, you could just keep spawning a background image  That's just doing crazy stuff and firing off or rotating over and over again  We have parallax as you see as the monkey moves up and down the actual clouds are moving with it  So you can say I would like that  That as this monkey moves keep it to be a specific I'll go back and forth so you can see it again  I want to parallax those actual clouds you kind of link those two objects together to move them around which is really cool  It's handled all for you automatically  We also have multiple effects so we're doing like wavy type things and spinning things  Automatically and we'll see that we're actually gonna set up some some effects actually spin this banana and move this ball around and move it around  on the screen and  Finally, we have an a very advanced physics system  In the actual game logic as well  So this actual box that's kind of moving it'll actually handle the physics of gravity for you automatically kind of in a world space  And all of these little samples that you're seeing here are all available  Actually on our github repo because everything is open source  We actually have one huge huge solution that has like every single sample in use case  So it's like if you want to try to do something we have a test for it  So that's kind of the core of cocoa sharp. There's a lot in it  It's a lot of text kind of on a slide  But what I wanted to do is just kind of build out a game and take a look  So when you get started and I'm gonna do everything inside a visual studio  You can do everything also inside of I mean I'm gonna do it in Xamarin studio  You can also do everything inside a visual studio and I'm just gonna focus on iOS and Android  But what you'll see in the actual business logic of the iOS and Android application as though there's three lines of code  So if you want to then bring your application to a new platform all you have to do is copy the image assets in  Add three lines of code and you have a game. It's just pretty awesome  So we kind of start and I like to start everything actually on  Github so this is the github and I'll zoom in here for the github. It's github.com  Mono  Cocoa sharp now we put this on the mono  Mono directory because it's kind of built on mono game. It's open source and actively people can contribute to it  Especially if they're on the mono project and that's kind of what we're actually doing  You know Xamarin where the creators and the maintainers of the mono project  Which is an open the original open source implementation a net and that's what we're actually leveraging here on iOS and Android to bring  That over to create iOS and Android apps. I'm gonna pull this down since this doesn't fall  There we go. Cool. So if you want to see what we're doing and have issues or how to pull requests or look at the wiki  Everything is here  So every single bit of the entire cocoa sharp library is actively here  Including you could load this up on Android Mac OS. So we have Ooya support  And then all different types of windows and windows 8 and phone support as well  You can see what we're doing in here. We have a forum  So if you want to actually go and actually maintain to it actually have questions  We're active there and we have this wiki and everything is under MIT license, which is really really great  On the wiki it kind of gives you everything how to get started  More links to the forums so we have a slack channel  And we even tell you if you want to deep-dive on why we created it what the core differences are and getting active  With the new get packages and look at what we're doing in the future as we evolve cocoa sharp everything is going to be documented here  Now what we did is we created some project templates  So I like to go over here and you can see there's project templates in our wiki and  The project templates all it does is tell you how to get started in other visual studio or Xamarin Studio  In visual studio all you do is just kind of download a project templates of the vsix file  Into the extensions and that kind of gets you good to go over there  Now if you're inside of Xamarin Studio Xamarin Studio is our IDE. It's built on it works on both Windows and Mac  It's a lovely lovely ID eats very similar  We built it with the visual studio developer in mind  So all the key bindings are the same across the board and it looks very much like an IDE  We have built-in source code analysis in it if you ever use  Xamarin Studio at all we have this thing called an add-in manager very much like visual studios add-in manager  But when you go over to gallery, there's a cocoa sharp entry and all you do is install the project templates  And that'll kind of get you up and running to do iOS  Android and Mac development for cocoa sharp and that's really convenient  So once you have that installed to get started with cocoa sharp you're gonna set new solution and under app  we're gonna see  Some brand new templates in here this cocoa sharp showcase and  Empty project so we're just gonna kind of do file new project see what that looks like so we'll say  And DC cocoa sharp there we go  Now we distribute everything through new get so  Everything is just a new get package and on up top here. It's gonna add a bunch of packages for me automatically  Over and over and over again. We'll probably even update them. Let's take a look here  That's good. Let's see what we're on 140 perfect  This is actually pretty nice it's gonna okay got everything awesome  So inside of here what we have is an Android project an iOS project and a common  Lib and that common portable class library is gonna handle all of our game logic of our application  And we can just see that there's just a cocoa sharp PCL inside of here  So the main app delegate that's here. This is gonna handle kind of our business logic starting our application  So we can see if the application finished loading and inside of here  It sets up kind of some default state for us to say hey, I have a content folder. There may be some animations fonts sounds  I really like this to be 1024 by 768, but it'll handle the scaling for you automatically on these different devices  If you're an Android developer at all, you know, it's a crazy crazy beautiful disaster over there of a bajillion devices  and we handle all that for you so scaling or putting borders around your game for you automatically and  Then here we'll actually handle a high density or low density images  And then it starts and creates a game layer as that child and it runs with that scene  So that's the start and we can see we even have a little pause state inside of this that you could check at any time  Then we have a game layer so this game layer is going to do a few things  It's going to load up some sprites and if we look actually inside of this content directory  Over an iOS. We'll see some images in here HD and Lodi. It's just a monkey. That's inside of here. There he is adorable  So it's gonna load up a few monkeys add it to this layer. So this this is a layer a CC  Great layer gradient that's in here  So that every single one of your screens will be some sort of layer a solid color you can add a background or this one's gonna be a gradient  We're gonna create some of these  CC actions and the default there. We have some move by we're gonna create a sequence of  Repeating it and we're gonna spawn this this this effect this wave effect  So I'm just gonna kind of run this so we can kind of see what it looks like  I'm gonna select an iPhone 5s simulator  Let's see what this looks like. Oh  Go ahead and spin up there  So something too too crazy  It just kind of shows you some things that you can do so loading some sprites putting them on the screen and kind of moving them around  We're also generating a particle system in the background and what I can see is if I want to interact with this  I can tap on the screen and that particle system moves  So I want to kind of show off the core of what we're gonna be doing when we actually build out a game  So up here what we'll see  Or is it that down here?  Run with there it is  We have this event so we create an event which is a listener to say whenever anyone touches anywhere on the screen  I could subscribe to multiple touch events  So there's on began on cancelled on ended on moved and whatever I lift up here  We go in and we run this touches ends and what we can see that all I'm doing is this galaxy system  That's creating all these particles. I grabbed the first touch event grab the location if it was moving  I stop it and I say let's move it somewhere else on the screen  And then I told to move over a certain amount of time so over this duration move it to this position  So really really simple and I run this action. So everything's action based inside of here. It is really nice  So that's what is going on here. So if I move this around we can see it just kind of moving around on the screen  Now if I load this up over on Android we can take a look over here. So let me go and pump up an Android simulator real quick  Now I'm going to be using our Xamarin Android player, which is a free  Emulator from Xamarin anyone can use it for any type of development on Mac or PC  It's all based on virtual box. I was really really fast  Which is really really nice. Let's see if this will spin up here. So we'll go ahead and pop it over here  Just make sure I have everything set up correctly in my build actions. Nope  No got an email perfect like that. There we go  And  What we see is actually in the iOS or Android application  We have our our actual iOS app delegate and this is kind of the start of the application  So I'm creating that shared app delegate and then over in I got to rotate it. There we go  Then we do the same thing on Android, but the same exact game same image assets everything like that now over on Android  I didn't actually write any Android code. I just everything in C sharp  Because I'm using Cocoa sharp  So if I look at Android over here, we can see that I'm using some packages Cocoa sharp Android in my main activity  What do you know? I just have a little bit of configuration and I start the game  That's it. So even if you don't know Android or iOS development at all  You don't really need to because you're just learning the Cocoa sharp API and it's just going to run over there  now if you want to tie in for instance into like Google Play Games or the the  iOS equivalent for what it's called game center game center. That sounds right  You can actually add that integration if you want to put on Amazon devices  You could download the Amazon SDK and put it right in there and the same thing would be true over on Windows  You would just pull in the Cocoa sharp Windows new get package add these same lines of code  And then you could integrate with anything you want if you want to integrate in Cortana or something like that  You absolutely could because you could just bring in those packages  So let's actually open up another game and actually build some things out from scratch and walk through kind of building out a game and  The kind of sample you saw with that little little monkey kind of moving around and collecting bananas  That's a game that we have called gone bananas. Go figure. We're big fans of monkeys as you can see this is Sebastian  He's up here hanging out with us today and and this gone bananas is available for every single platform  Windows iOS and Android and I've just opened up the iOS and Android bit here and what we're gonna notice is that we still have an  Assets folder and we have content inside of here. I added some custom fonts into my application  Some sounds I have an mp3 and I have some wave files  And then I have some I've an animation so if I open up this monkey here. This is that same thing  We've even stuck the banana in there so we can pull it out just a sprite sheet  And then I have this p a p-list which was generated by a tool for me if I open this up  think  Here let's try to open it  There we go, it's just keyframes so it says what frame do you want to use per so it's just like a  Spits that out for us and then we have the balls  There's the balls that we're gonna be dropping from the sky and the idea is that these bananas and balls are gonna fall from the sky  We want to collect the bananas if the if the actual balls hit us then the game's over  But we also have this background that we're gonna use grass and that'll be kind of nice all right perfect  So that's everything that's inside of my core application if we look at the app delegate again on iOS and Android is gonna be the same  Except we're gonna create this gone bananas app delegate  And that's what's gonna start here, and I want to write a few hundred lines from scratch  So some of this code is in place some of it we're gonna write out, but it's gonna look very much the same  We can see that I'm preloading some effects sound slash taps sound slash background music  I'm just grabbing the main window size and setting that as the default and I'm running the game and we're running this start layer  So there's three  Three screens to our game. There's a start screen a main game loop that's gonna run all of our business logic and an end game  And we can loop those back and forth so the start of our game  here  If I open up the main game layer  It's pretty pretty simple  I have a click handler and all it does is whenever I touch anywhere on the screen it'll start the game  It's gonna start my main game layer, and I just add that touch listener  I have a black background and all I do is put a label we create a CC label and you can by default  Just slap down some text load up custom  Fonts and we can see I have aerial in here because I brought an aerial you set  Specific size and then you say where you want to do it. I'm just gonna align it in the center and add that in  That's it  The the main game layer that we're gonna interact with here is gonna do a bunch of stuff and we're gonna kind of walk through this later  But when this is over what we're gonna have is a game over layer and these two are kind of already caked and all this one  Does is it looks very similar to our first game, but it takes in a score so I could display a score on the screen  on there and  I have a score label and a play again label whenever they tap it  It just brings them back to the previous screen to start a new game. So very very simple inside of here  That's it. All right  So let's go into our main game layer and that's where all of our actions gonna be and I set up all my variables ahead of time  So how fast do I want the monkey to move?  How long does my game last which is gonna last 60 seconds and how many maximum amount of balls do I want on the screen?  We have some physics. I'm not gonna go into physics logic. So I'm not a physics major  I just kind of put that in a helper class, but we have a bunch of physics that's gonna happen  And then we can see what else I'm doing here  So first I'm gonna set up what I want to track in my game. So the elapsed time so after 60 seconds  I should end my game that's gonna be a goal. I'm a main monkey and I'm gonna interact with so it's gonna be a main  Sprite, he's just gonna move around and animate on the screen  And then what I'm gonna do is I'm gonna keep a list of visible bananas and hit bananas  So the idea here is that I can just kind of track and actually see the rotation and check the collision of any visible  Bananas on the screen and then the hit bananas will go into a list that I could track later that I've removed from the scene and I can  Get a count for them later on  And then I'm gonna kind of start setting up my game and some of these are just  Animations that'll be played and we'll set up so we have a walk animation and we'll set that up in a bit a walk repeat  So it'll loop forever  Animation and a way of stopping it. So this just says hey on any node stop all actions  That's a little extension method so you have a way of saying if you're moving stop right now  Just stop walking stop doing whatever you're doing. I  Also have a sprite just like we had our monkey. I have a grass. That's gonna be our background sprite. I  Have a rotation so I say over point eight seconds rotate the banana  360 degrees we handle all that logic for you don't have to worry about it  We'll take your sprite and rotate it just with this action and  Then what we do is we I set up a little action just like we said stop all actions when I want to stop walking  Also remove this banana when it's done. So I'll remove the node that banana node from the parent  So I'll just remove it from the game scene if I collide with it  And that'll be any any node and then I set up my balls that are here in my physical world  So I have a little bit of logic just initializing  And we're gonna walk through adding some things side by side  But I just wanted to do a little bit of setup here  So I might a touch listener touch all at once and that's whenever I end and pick it up  I set my background color to white initialize my list and in this instance  I'm gonna create that sprite batch so those balls were in a batch file basically inside of there  I'm just gonna pull down that texture and split it all up. So I have my balls ready  And I'm gonna add grass add monkey and start scheduling these are all blanks  We want to fill those in  Now all we need to do at this point is actually start adding some things to the screen  So let's go ahead and do a few things  It's out here. Let's remove our monkey so we can kind of add one thing at a time  so we're gonna add our grass and  Our grass is gonna equal a new cc sprite  And it's gonna take in a file name. That's gonna be grass  There we go  I should say let me just run this first. You can see what it looks like. I think this should run  Let's take a look  So I've really done anything just initialization and hopefully if I didn't crash anything it shouldn't do anything  Perfect there's  Tap screen to go bananas and it's white. So that's what we have that's our game right now  We're gonna transform this over the next 20 minutes  So we're gonna add some grass  So first we'll do is we'll initialize that grass and then we'll say add child to this layer and there's grass now  I do get some events. I get my application event lifecycle  But each layer also has some event life cycles as overrides  And we're gonna see whenever this was added to the scene whenever this this layer was added to the scene  I can initialize a few things  So I'm gonna go ahead and position my grass right in the middle so we can run this again  So I create my grass add my child and run it again  Grass that just happened two lines of code not bad. Let's put our let's put our monkey down on the screen  So here what I'm gonna do is this is a little bit more logic  So I didn't want to write this from scratch, but I'll walk through it with you  So instead of creating a sprite this monkey is a sprite sheet with all those animations  So what I have to do I'm gonna pull this down a little bit. We have a little inline function here  Let's say load up that animation  Peelist info that sprite sheet that's gonna have the different keyframes and  Grab all the animations that start with frame because frame one frame two frame three frame four for each animation  And then I'm gonna initialize a few things  I'm gonna animate and create an animation  So I take all those keyframes and I say each frame should last point one second. So we kind of moves across the screen  That's the main animation and then I'm gonna create a repeat  So just keep animating that walk animation over and over again  So there's an animation and I tell the action is to animate the animation and repeat that forever  So just kind of always keep doing that and the last thing that we're gonna do is create my monkey  Based on just the very first sprite in that list  give it a monkey name and  And then I scale it point two five percent. It's a really big monkey and then I add that monkey to the list  That's it. So we have our animation that's gonna be looping over and over again, and I create my monkey  So now I can also put the monkey right in the middle load it up again  Now we got a monkey  But now of course we want to do something with the monkey because if I tap on the screen I'm actively tapping nothing's happening  So let's move them around a little bit  So we saw earlier if we look up top I created this on touches end  Here it is. It's blank. I have it down there in case of emergency. We're gonna try to type this out  So every time I move we want to move that monkey to a new location and we want to animate him  So the first thing that we're gonna do is I'm gonna say a monkey  Let's stop all of your actions. So if you're moving if you're doing anything if you're rotating for some reason stop doing that and  calm down  Then we're gonna grab our location and that's gonna see we have a touch event  So you can see which touch event came in but also a list of CC touches  So we'll grab our first touches out of that list and we'll grab the location  on screen so the physical location on screen and  Then we what we need to do here is we need to take that location  And we're gonna call a method called world to screen space  And what that's gonna do is it's kind of take the current location on the screen and put it into our world that we have here  So I can manipulate it  Now we have to do a few things  We don't want all of a sudden to say move that monkey over there in point one second and he goes racing across really fast  Or maybe he's only moving just like a few pixels  So we need to calculate how fast this monkey should move. So what we'll do is we'll create  The distance we'll calculate the distance here. So let's say distance  And we have some helper methods  Inside of here on our point. So there's a point x and y coordinate. There's a distance  We can calculate the distance between two points. That's pretty pretty lucky  So we'll say monkey position where you're at right now to our location and we're good to go  And then we'll say speed  There and we'll take our distance and we're gonna divide that by our monkey speed  So it's gonna kind of normalize our speed over distance ratio  I may not be the best math in the world, but it works for the sample  Now we need to actually go ahead and actually  Move our monkey. So we have some moves in here. We can move by move by state move to move to a state  So we're gonna move to  over we'll do it over that  Speed all right. Sorry. This is calculating the time. That's my fault. Sorry time  The distance divided by speed that equals time. Yeah, I think calc math. It's been a long time  So one thing I forgot about game development when I was starting all this like oh math  And we're gonna move that to the new location there we go  And we're gonna tell our monkey a few things we're gonna tell our monkey to start animating  We're gonna tell it to move and then at the end of it. We're gonna tell it to stop animating  So we'll first say run action monkey. Oops. That's not it say move to  there we go  monkey dot  start  Where'd it goes run action? There we go run action. There's run action async run multiple actions  And we're gonna tell it to walk repeat  Then we'll say monkey dot run actions  So we're gonna run two in a row so this will pair them up so it'll always walk repeatedly  And then we're gonna run some other actions  So we're gonna say move to first and then we have a walk animation stop  So we're kind of chaining up multiple events. We're gonna say hey run this first animation and keep running it  That's our animation that's looping over and over and over again for him walking and animating and then run the action to move to that location  And then what this does is when we're done it'll run the next action  Automatically, but we're running them in two separate calls. So it's always walking and repeating over and over again  So let's run that again  There's our monkey I'm gonna tap up top boom if I zoom in you can see I'm moving around pretty adorable  pretty adorable  alright  And that's your game done. I'm just kidding. Alright, so what do we need to do next?  So the next thing that we need to do is actually schedule some things  So we've added our grass added a monkey and then we need to schedule some things and what does that mean?  Well every game that you ever create has a main game loop the game loop is always running every single frame and you need to update it  So what we did is we enabled a way of  Scheduling things and you can schedule it's kind of like a task-out run almost  What we said is keep running this task at certain intervals in time and you can schedule up a must a bunch of actions over and over and over again  So if I come down here  After this we have this start scheduling and I kind of lined up what I need to do  So the first thing I need to do here and I start scheduling is I need to schedule a banana  I need to schedule to add a banana to the visible bananas list  I'm gonna add some time to our game loop. So I'm tracking up to that 60 seconds  Over and over again. I'm gonna check to see if the game is over and then go from there  So down here. I created a few helper methods. We have is time up pretty simple math is the elapsed time greater than the game duration  And then I also had this add banana thing and what this is gonna do  It's gonna load up our sprite sheet take out the banana  That's in there. It's gonna use a helper method to start in a random position  So I have a bunch of helper methods that I'm not gonna go into it's a bunch of more math and logic and shenanigans  So we'll just pretend that I went over that. It's all the other stuff's online  We're gonna grab a random position  We're gonna start and here's the banana on the screen at it and then similarly like how we're removing the  Actual monkey on the screen. We're gonna do the same thing. We're gonna move the banana down to the bottom of the screen and  Then when it's moved to the bottom of the screen remove it from the view and then always rotate that banana over and over again  So you're gonna see it spinning on the screen  So let's just do that. We'll come up to our schedule and we have a keyword called schedule  Now don't be worried. We're gonna actually put some inline actions inside of here. We're gonna use this one  It's gonna take a float which is the time that it was took in between the last call and an interval  So we'll say t and we'll just put a little stuff in here and we'll do auto 1.0 f  So every second we want to add a new banana to the game. So we'll say  Visible bananas dot add we're gonna call that add banana  there we go  We're gonna say our elapsed time plus equals t  That's a time difference between there. So one second two second three second  But it's it's always one second so we could change this and this could be variable  then what we're gonna do is say if is  Time up  Let's end game  Now this end game method is down here and I haven't done anything  We're gonna do that later  But our core business logic is in place and we can start it up again and just see what the scheduler is gonna do over and over and over again  And for lucky a whole bunch of bananas should fall from the sky  Boop  And that's pretty cool so far and just doing it all randomly so I'm kind of getting a random x in there. That's pretty cool  Alright, so let's do another thing. Let's go ahead and schedule  another one so we'll say t and we'll come in and we'll add a ball now  In here, we'll do that every I'm gonna do every point one five seconds because it's pretty hard with point one and I have a  add ball  method and  That of course is gonna be over in my little helpers class and what this is gonna do is it's only gonna add the ball if  If we have don't have our maximum amount of balls in there  So the balls are already is gonna kind of be there. It does a whole bunch of  2d vector stuff to kind of like bounce around so it bounces up and down off the physical world space  That's created more math. Don't worry about it  Games don't worry about math. It's not important. So we'll run that again. It's actually very important learn learn lots of math  Cool so we'll tap again and now what we'll see is now we have a ball  But the balls are just kind of the top of the screen because we haven't animated it at all yet. So we need to do that  But they're there. They're they're occurring. That's interesting  Boom. All right. We're almost there. All right, we could do a few things. We could schedule  Every second to do something. We'll say hey just every or every frame. It could take in just an action  We'll say check collision  inside of here now check collision  Not to do T  Just do this to make it easier on myself. There we go now check collision  There we go is another helper method that I wrote but it's actually really not too difficult  What we have inside of here is  bounding box transformations now  Bounding box might not be the best because you know, it's a monkey with a banana  But you could actually we have actual documentation of how to actually put multiple bounding boxes around that monkey  So you might want to do the banana than the actual body inside of there  But for simplicity's sake I'm saying for each visible banana in the list  If the bounding box that's transformed around it enters interacts with the monkey then that banana is hit  We're gonna play a sound effect called sound slash tap. We're gonna explode that banana  We actually do a particle and we'll look at what that looks like and we're gonna remove it from the parent  And this explode thing here is  Is actually a particle explosion. We have a bunch of them caked in automatically  But this one just says create and do an explosion of particles create 10 of them and automatically remove it from the scene when you're done  And that's it  But really just doing collision just looking at the core collision that you have like that's pretty simple math  Just every frame loop through my visible bananas and be good to go now, of course kind of best practice  If you have a thousand bananas on the screen, that's gonna be pretty difficult to check every frame  So you might want to actually do some more logic inside of there with like link or something  because links super fast, right?  so  So we'll check collision and we'll still see that these balls are up top that's that's cool  But we should this gonna like make the game really really great  Because we're only just checking the bananas. I'm just gonna win forever  So let's see here. So now if I actually tap here, boop  It makes the game easy to win  Perfect so now we got some collision going on we got some particles happening, which is a few lines of code  Now we need to schedule a few other things this one's a little bit more difficult  Because what we're gonna do is we're gonna loop through all of these balls on the screen  We're gonna do the same kind of detection to see if I actually am inside the physics body of the specific ball  And if so, I'm gonna remove that sprite and then we're gonna actually  move around on the screen that's a little bit more complicated  But this is gonna move the ball and destroy the balls when they're not moving  So it's a little bit more math inside of there. So let's take a look here  So now our balls are falling  And that's bad. Let's see if I put any collision not no collision yet. Cool  Perfect  So now what we need to do is actually  Do a few more things which is end our game. So we have our balls  We have our things occurring inside the game and then we can actually end our game if we hit a hit a  physical  But or a ball on the screen so unschedule everything unschedule all  We're gonna create a new layer. We'll call the our game  over  Scene with score. We're gonna pass in a main window  There's our window and we'll take in our score and that's gonna be our  Bananas no, it's called hit bananas dot count. So how many we hit and then what we can do is say  What do we have to do it's the director thing  Sometimes I just forget core logic window. Default director there we go window  dot default director  dot scene  Replace scenes or it's gonna move and we're gonna move to this new layer or scene if you will and that's actually should be seen  There we go. Perfect. And there we go  So that should be good to go now I need to double check to see why my check collision is here  Hit bananas ball count good to go balls hit count end game great. All right. Ah  So check collision it does a little bit more than that it adds in it  Hits updates the hit and then it also does some detection here on the the bounding box of the actual  Balls that are on the scene. So now we can move this again  Now  Ideally I should be able to kind of get some here and boom  I hit the ball that's interacted with it and I got two bananas if I tap again. I start up a new game  And we're kind of good to go now. It's kind of boring because there's no background music  That'll really spice this up. I'm pretty sure so let's go ahead and add that  So I'm gonna come up to the top  Way up top here into my game layer. I'm in the right scene. There we go  And after we set and start scheduling we're gonna use this thing called cc simple audio engine  We're gonna use the shared audio engine. So anything that's kind of all the sound effects and our game music  That's there. We can adjust the volume. We can also play background music  And if we take a look inside of here, what we'll see in our content sounds  We have background music. So we'll say sound slash background  Music mp3 and we'll loop that why not  Boom thousand times better basically best game ever. It's pretty cool. Not even moving  I'll see how you just gonna dodge all the balls. That's a pretty awesome monkey right now. I'm just like, oh, there we go  Pretty good music, I think we've all that for like a dollar  All right, so what's really cool now is that I can now just set Android as my startup project here and  Actually select that same Android device the emulator that I had there. Let's rotate it back. This is a portrait game  The same exact game same particle physics everything is reused a hundred percent across the board and this is scaled down  Obviously so I sick ale this up, but it's a full another game on Android  No additional work that I needed to do because everything is in my core  Portable class library with Cocoa sharp, which is pretty mind-blowing to me overall  And you kind of have a full game with just a few lines of code some math and some physics  But besides that it's all there. We can all figure out physics. It's easy, right?  well  Now all of this source code and more is all available online and I'll have these slides on my slide share  But you can just always tweet at me, but it's also all on that Cocoa. So when you go to the mono slash Cocoa's  Sharp repo on the mono GitHub. There's also the sharp Cocoa sharp samples and there's tons of games in there  Not only just this one, but there's an extension to gone bananas that actually has Apple watch support  So you can control the monkey from the watch, which is really crazy  Which is really cool. It's actually published. It's called gone bananas. It's on iOS and Android by one of our evangelists  a previous evangelist Mike Bluestine because he created this sample, which is awesome a whole huge walkthrough of creating the step-by-step  I didn't even go into the parallax or creating the Sun with the spirals and the background everything like that  There's a lot of cool additional stuff that you can create. I'll let everyone take a second there to take these snapshots  I'll tweet these out later as well. I'll have the slides online  If you follow me on Twitter  Now a lot of people ask about pricing like what's free. What's not free? Well Cocoa sharp is open source  And if you want to create Windows applications, you don't pay us for that  You pay Microsoft for whatever you want to do if you want to create games with Xamarin and Cocoa sharp  Does cost some money some subscription to do that  We have an indie edition starting at $25 a month for iOS and Android to publish your game  But to do it on Windows or anywhere over there, you're good to go once you have your Android or iOS application  You're paying us for the compiling the technology to bring that over and build the iOS and Android app  We haven't gotten sorry with Xamarin just playing around with it. You go to Xamarin.com. We have a full 30-day trial  I did mention Xamarin University earlier. We have tons of great stuff. I have some Xamarin stickers and plural site stuff up here  There's tons of great resources including our documentation to get started, but we just yesterday  I'm super excited that we did it yesterday and not tomorrow  But we have a full 30-day trial of Xamarin University  Which is live interactive and structures that go online and a go-to meeting and you learn 24 hours a day seven days a week  And it's like you're like a student you become a fully Xamarin certified developer  The free trial gives you a few courses and access all of our guest lecturers  I was just listening to Miguel decaza our CTO. I one of our guest lecturers talk about just the state of net is really intriguing  I did that before I went to bed  Which is really cool  So I recommend everyone checking out Xamarin.com University because it's free to do a free trial and there's no credit card or anything that you need  Of course, you can find me at any time just at James at Xamarin.com and at James Monta  Magno nice short and sweet Twitter handle  And I have a Xamarin form session in this room at 1620 the very last session the day. Yeah, so with that  Thank you guys so much  I'll be around as well up here if you guys have more questions. I have some plural site stuff and stickers as well
Making_2D_Games_that_Run_Everywhere_using_0_539.png Making_2D_Games_that_Run_Everywhere_using_540_863.png Making_2D_Games_that_Run_Everywhere_using_864_971.png Making_2D_Games_that_Run_Everywhere_using_972_1919.png Making_2D_Games_that_Run_Everywhere_using_1920_2651.png Making_2D_Games_that_Run_Everywhere_using_2652_9179.png Making_2D_Games_that_Run_Everywhere_using_9180_9683.png Making_2D_Games_that_Run_Everywhere_using_9684_10499.png Making_2D_Games_that_Run_Everywhere_using_10500_10655.png Making_2D_Games_that_Run_Everywhere_using_10656_10919.png Making_2D_Games_that_Run_Everywhere_using_10920_14087.png Making_2D_Games_that_Run_Everywhere_using_14088_15131.png Making_2D_Games_that_Run_Everywhere_using_15132_15431.png Making_2D_Games_that_Run_Everywhere_using_15432_16559.png Making_2D_Games_that_Run_Everywhere_using_16560_17135.png Making_2D_Games_that_Run_Everywhere_using_17136_19175.png Making_2D_Games_that_Run_Everywhere_using_19176_21623.png Making_2D_Games_that_Run_Everywhere_using_21624_23807.png Making_2D_Games_that_Run_Everywhere_using_23808_30083.png Making_2D_Games_that_Run_Everywhere_using_30084_30383.png Making_2D_Games_that_Run_Everywhere_using_30384_30671.png Making_2D_Games_that_Run_Everywhere_using_30672_31907.png Making_2D_Games_that_Run_Everywhere_using_31908_32735.png Making_2D_Games_that_Run_Everywhere_using_32736_32783.png Making_2D_Games_that_Run_Everywhere_using_32784_32915.png Making_2D_Games_that_Run_Everywhere_using_32916_33251.png Making_2D_Games_that_Run_Everywhere_using_33252_33803.png Making_2D_Games_that_Run_Everywhere_using_33804_34271.png Making_2D_Games_that_Run_Everywhere_using_34272_34631.png Making_2D_Games_that_Run_Everywhere_using_34632_34751.png Making_2D_Games_that_Run_Everywhere_using_34752_35195.png Making_2D_Games_that_Run_Everywhere_using_35196_35519.png Making_2D_Games_that_Run_Everywhere_using_35520_35567.png Making_2D_Games_that_Run_Everywhere_using_35568_35651.png Making_2D_Games_that_Run_Everywhere_using_35652_35711.png Making_2D_Games_that_Run_Everywhere_using_35712_36311.png Making_2D_Games_that_Run_Everywhere_using_36312_36539.png Making_2D_Games_that_Run_Everywhere_using_36540_36803.png Making_2D_Games_that_Run_Everywhere_using_36804_36947.png Making_2D_Games_that_Run_Everywhere_using_36948_37259.png Making_2D_Games_that_Run_Everywhere_using_37260_38291.png Making_2D_Games_that_Run_Everywhere_using_38292_39767.png Making_2D_Games_that_Run_Everywhere_using_39768_39923.png Making_2D_Games_that_Run_Everywhere_using_39924_39983.png Making_2D_Games_that_Run_Everywhere_using_39984_40847.png Making_2D_Games_that_Run_Everywhere_using_40848_41003.png Making_2D_Games_that_Run_Everywhere_using_41004_43127.png Making_2D_Games_that_Run_Everywhere_using_43128_43331.png Making_2D_Games_that_Run_Everywhere_using_43332_43391.png Making_2D_Games_that_Run_Everywhere_using_43392_43523.png Making_2D_Games_that_Run_Everywhere_using_43524_43619.png Making_2D_Games_that_Run_Everywhere_using_43620_44435.png Making_2D_Games_that_Run_Everywhere_using_44436_47531.png Making_2D_Games_that_Run_Everywhere_using_47532_48671.png Making_2D_Games_that_Run_Everywhere_using_48672_48719.png Making_2D_Games_that_Run_Everywhere_using_48720_56459.png Making_2D_Games_that_Run_Everywhere_using_56460_65939.png Making_2D_Games_that_Run_Everywhere_using_65940_66131.png Making_2D_Games_that_Run_Everywhere_using_66132_66251.png Making_2D_Games_that_Run_Everywhere_using_66252_70691.png Making_2D_Games_that_Run_Everywhere_using_70692_75623.png Making_2D_Games_that_Run_Everywhere_using_75624_75695.png Making_2D_Games_that_Run_Everywhere_using_75696_76943.png Making_2D_Games_that_Run_Everywhere_using_76944_79943.png Making_2D_Games_that_Run_Everywhere_using_79944_81635.png Making_2D_Games_that_Run_Everywhere_using_81636_81683.png Making_2D_Games_that_Run_Everywhere_using_81684_82115.png Making_2D_Games_that_Run_Everywhere_using_82116_82475.png Making_2D_Games_that_Run_Everywhere_using_82476_82619.png Making_2D_Games_that_Run_Everywhere_using_82620_83099.png Making_2D_Games_that_Run_Everywhere_using_83100_83435.png Making_2D_Games_that_Run_Everywhere_using_83436_83687.png Making_2D_Games_that_Run_Everywhere_using_83688_83747.png Making_2D_Games_that_Run_Everywhere_using_83748_83807.png Making_2D_Games_that_Run_Everywhere_using_83808_85055.png Making_2D_Games_that_Run_Everywhere_using_85056_87239.png Making_2D_Games_that_Run_Everywhere_using_87240_88175.png Making_2D_Games_that_Run_Everywhere_using_88176_88319.png Making_2D_Games_that_Run_Everywhere_using_88320_88411.png
Awesome look at that fancy transition. Whoo. That was awesome. So how many of your C sharp developers? Awesome, you're all game developers that just happened. I just turned you all into game developers How many of you guys have ever made games at all like 2d3d? Quite a few how about like for councils a Few all right you guys win So a long time ago 2006 I lived in Phoenix, Arizona And I wrote a lot of C++ code and I made a game for the Xbox 360 I had it dive deep into the Facebook archives actually find it and a little video. It's an FLV Whenever that was made so here's my history of video games So I did this with super sweet music So that took about two years and I did that for a small company and then I said okay I'm never gonna do that again Like that was the worst experience in my life who wants to write C++ code over and over and over again So I learned C sharp and then I wrote printer software for four years games to printer software natural transition It's fantastic. I really like to say that you lead need at least four years of Enterprise printer software under your belt to put software engineer on your LinkedIn page It's just a given like you have to have it. So what happened? Well, and who am I? Well, that's me. I'm James Montemagno I'm a developer evangelist at Xamarin I've been creating and crafting dotnet applications for the last decade after ever since I moved out of the game industry And I live in this wonderful place. It's just like Oslo, but it's called Seattle. It's lovely. It rains there, too We got that it's cloudy, but it's also beautiful I'm blown away when I got off the plane my girlfriend says how's Oslo? I said it's just like home and it's actually really true great coffee great people just actually really really beautiful But I moved there four years ago because I said enough is enough with printer software Because really you can only take about four years of printer software any software developers writing printer software right now Perfect you all you all nailed it never do that never do that It's actually it's not that bad you learn great enterprise like when you know are porting a legacy dotnet 11 application to 2.0 to 3.5. You're like, that's great So I said enough is enough. I'm gonna move to Seattle not only because we have the most amazing coffee everywhere besides Oslo Maybe I'm going to Tim's tomorrow. I'm very excited about that like I came to Oslo I was like, what do I need to do coffee like oh go to Tim's very excited But I moved there because I love mobile development. That's been my main focus I went to the final PDC before there was fancy builds and everything like that got my hands on a Windows phone I was like, oh my goodness there's so much power in the palm of my hand and I found Xamarin because I was a C sharp developer And I've been crafting iOS and Android and Windows applications for the last four years and every single thing that I do Is open source complete on my github? I can find me anytime on github on my blog at James as Xamarin.com or really just on Twitter That's the easiest way to get a hold of me. I'm up probably 24 hours a day probably on a plane somewhere flying somewhere I like to say I code 50% of the time on a Plane 50% of the time presenting 50% of the time and helping all of you guys 50% of the time Create create awesome mobile applications. So I'm kind of 200% always on so we're here to talk about Cocoa sharp and building 2d games all in C sharp I'm gonna tell you a little bit about Xamarin. How many people have no idea what Xamarin is a Few per is always like one or two so Xamarin I have a talk later on Xamarin itself with Xamarin forms building like native Applications for iOS Android of course windows all in C sharp And the Xamarin platform is kind of actually what powers Cocoa sharp in a way It's enabling us as C sharp and dotnet developers to get away and still create windows applications But come over to iOS Android and Mac and use all of our dotnet and C sharp skills and libraries to create great native Applications that's a Xamarin platform. You can do that in visual studio or Xamarin studio RIDE We also have a great testing product called Xamarin test cloud to do automated user interface testing for any of your iOS or Android apps Across thousands of physical devices. We even have a crash of litics and analytics software for your mobile applications and Entire training online university called Xamarin University go figure It has a free trial actually so you can actually go take a few classes for free and learn all about Xamarin and creating iOS and Android apps And how this all works and kind of even how the Cocoa sharp bits works is the same way that Xamarin works We need to build out a shared C sharp back end for any of your mobile applications And these are all your business logic of your application. So your models your view models Your SQLite databases your calls to Azure to AWS or wherever you want to go any of your dotnet code your things that your Applications are doing the same on each platform Then we give you the tools or frameworks to build on native user interfaces for iOS Android and Windows now I usually talk about creating just mobile applications themselves either for the enterprise or consumer But you can think about this iOS and Android and Windows UI up top there as just a game UI and that's where the Cocoa sharp libraries gonna come in and replace that Native kind of storyboard or Android XML with actual 2d game engines and user interface So that's what we're gonna talk about today And there's actually several ways of building games with Xamarin not only just Cocoa sharp So if you want to tie into the actual native platforms or in 3d as well, there's ways of doing that I like Cocoa sharp Because I never thought I'd go back to games and then I went to a game jam and created a game in like 12 hours And I was blown away and I'll show you guys that later on But we have mono game and that's actually the core of actual Development it's a it's a it's a port of XNA over to iOS Android Mac and even Linux that powers tons of amazing games like Bastion and Towerfall and a bunch of amazing games out even on like PlayStation like transistor for instance So that's all in C sharp. We also have a mono for unreal, which is a project that we've been working on It's just for desktop at this point in time But if you're kind of in the unreal engine type of space, there's mono for for unreal so you can write everything in C sharp Which is very cool Built into core iOS is a few API's that Apple gave to actually create Native 2d games and sprite kit is one of those if you use Xamarin to actually create your native iOS and Android applications You get access to all the API So if you could do it in objective C or in Java and you can do it in C sharp So we expose sprite kit and also scene kit as a way to build 2d and 3d games just for iOS But I think Cocoa sharp is kind of like a sweet spot in between because it blends the paradigms of what we kind of want to do in 2d games with C sharp idioms and Miguel kind of wrapped it up like when people ask what is Cocoa sharps? And he says it blends the power of the Cocoa's 2d Programming model with C sharp and the dot net framework. So leveraging C sharp and dot net And then what we did is we took what we kind of loved about Cocoa's 2d And we added the C sharp and dot net idioms on top of it So if you ask Miguel why why did we create C sharp or Cocoa sharp as part of Xamarin? Well, this is why because we love the ability to kind of create 2d games with Cocoa's 2d But we wanted to do it in C sharp in a way that dot net developers would kind of know and love with events and and all the great Things about dot net in the back end. So how many people are familiar with Cocoa's 2d at all? Nobody, okay, cool So well, let me tell you a little bit about the history of where Cocoa's sharp came from Well, Cocoa's 2d is a 2d game engine and it was built in object for dejective C developers So they create Mac and iOS applications And then that got ported into the cross platform space with a Cocoa's 2dx Which was all C++ based? Well, that's that's great. Everyone loves to manage memory So then someone said there's got to be a better way So someone then went out and leveraged monogame Did already be out and created Cocoa's 2dx na which was all in C sharp But it leveraged new platforms across iOS and Android So the first two there the first one only targeted iOS and then Cocoa's 2dx targeted Android and iOS and even Mac desktop and then Cocoa's 2dx na which has been out for quite a long time Targeted all the platforms Now what we did with Cocoa's sharp was that we took what was there in Cocoa's 2dx na We kind of loved what was going on there. Well, we wanted to contribute actively to it So we kind of forked it and there's some logistics of putting things back into a different licensing So we said, okay, we're just gonna open source all of Cocoa's sharp So it's available completely on github We're gonna do a few things that we thought just could be nicer For C sharp developers. So we did we fixed constructors and naming and different dotnet idioms And I like to call it C sharpier. It's a C sharpier API We had global state so before you can only kind of one windows director and that's kind of what we'll talk about here Is but you can actually have multiple screens going on so if you're in the desktop space actually have a multiple screens I mean actions immutable. We added new get in PC Lified everything pickled it if you will Added extensions and we like to call sanitizing the hierarchy. You know go over the hierarchy of a game But we did lots of other things in a cross-platform way like events and colors and labels to just simplify the API So anyone new coming in it would be a little bit easier just to kind of get on board So the Cocoa sharp API is actually quite simple and I'll just take like a few minutes before we jump into kind of building out a game To kind of go through the essential parts. You kind of know what I'm doing when I'm building out a game So the scene hierarchy you could think of anything on the screen is an active screen And there's a few steps that you have to do to get there So you have an application It's any application and that application as a director and that director what it's going to do and we'll step through this is Going to direct a few different elements of what's on the screen and usually it has one scene at a time So you usually have one application one director and one scene as you normally what you would expect in a game on a mobile device You're not having multiple windows or anything like that But since we kind of changed the API a little bit you could actually have multiple directors here now these scenes have multiple layers and each layer is consisting of Certain elements such as a sprite a label or a menu that might be on the screen and then All those layers can have multiple sprites So for instance you have multiple characters or multiple labels on a screen or multiple items in your menu So this is kind of the hierarchy of the game and what we're going to add on to is building out some sprites Which can be a flurry of different things like particle effects or just animations and you can interact with these different elements as well So the CC application so you'll see all over the place the the core Idiom that we put in place is CC so Cocoa sharp or Cocoa's so everything was Cocoa's so it's Cocoa's application Cocoa sprite so you'll see that over and over again in the actual Code that we'll be writing but the application itself is going to do the hard work for you Well, we wanted to ensure that you shouldn't have to worry about initializing Graphics drivers or things specific for the device or worry about open GL or whatever it's running on Because Cocoa sharp no one runs on iOS and Android, but it runs on Mac desktop It runs on Windows or runs on Windows phone Windows store. It's like wherever you want to put it It's everywhere you want to go but it handles as Application delegate and actually this is the core the beginning point of your game And the delegate here is similar as anyone done iOS development a few has anyone done Windows development It's like your app.cs. Basically. It's the start of your application so all your lifecycle events for your core application and This delegate it's similar to iOS UI application delegate, but it does a few things It enables us to point to say this is where my images are my sounds are at so Cocoa sharp can reference those later and then it also Sets the first scene, but it also gives us a lifecycle activity events So we can see when our application enter the foreground when it finished launching for the first time or when it goes into the background So back and forth so we get some lifecycle activity for our entire application and this is important if you're gonna Basically pause your game whenever your user maybe exits out of it or hits the home button or a phone call comes in You're gonna get this event and you're gonna pause things So a great thing that you probably want to do is pause the sound. That's probably one thing you want to do Or additionally if you're gonna save some state of your actual game, that's where you're gonna do it at is all in this application delegate Now the content folders I reference that the application is gonna have these content folders We're gonna put some things inside of there and we're gonna put some fonts sounds images You can even create complex animations with sprite sheets and also particle systems So if you have pen particle generating tools that put out like P lists Which kind of specify what is going on in the particle systems? You can put them right in there and we'll take a look at those as well and then Every application you just pointed a route and then you can say here's the search pass underneath it And you can even preload sounds ahead of time So for instance if you have an animation or the first screen of your application that you want to show and you want to launch immediately You can say preload this content ahead of time So your your assets load faster if you don't do that and you just want things to load on demand like you're loading a game level So then load all the content you'd want to handle that separately per level That's there, but if you have a simple game, maybe one or two screens you could load everything up front Maybe even just put a loading screen Then we have this thing called the director and this is kind of that first bit now We're broken away from the application now We're in the director and all it does is handle loading scenes So you can replace screens switch back and forth between screens and what we'll see here is you know You might have a launch page and then you need go in your main level you go to level two You go to level three and you keep directing if you will kind of like a movie producer You're just directing and what the scenes are going to take place and those scenes Are the various logic portions of your application and these all contain cc layers. So each layer of your application So really you're gonna handle mostly deal with a layer the scene is only used By that director to point where you want to go So that layer for instance is really important. This is where you're gonna do all of your business logic You're gonna handle your game loop your main game loop. That's always running We're gonna schedule Activities to occur so maybe I want to check collision every frame or every second Maybe I want to spawn a new enemy my you know detect when a user is moved into a certain region and then react to that And and what we're seeing here in this little bit of game logic is that we're creating a new cc scene with the main window Every application has a window We create this start layer that's there and then we add it to the game to you know to the scene So that scene could have multiple children layers on top of it that you could direct around And then those sprites so those layers are gonna contain sprites And there's multiple sprites that you can have you can have sprite sheets So we see the monkey on the bottom there. He's actually an animation sprite So he's actually wobbling back and forth kind of like this if you will little mono monkey so he's gonna be doing that and These are all gonna be inside that content folder and that's what's really important We've standardized on this content folder So you add your images on Android on iOS on Windows you can optimize the the The density if you will so even on iOS you could do at 2x at 3x so you can handle retina displays and same thing on Android You could actually handle HD images if you're going on a high resolution device for instance and Each of these Sprites that we're seeing here is just a cc sprite and we'll load them in different ways And what's interesting here is you have a cloud and you have like this ball here and this ball is also just a Sprite sheet, but it doesn't have any animations You could add everything into one big sprite sheet if you've done any game animation you kind of just you know snip it all out so that way You don't have multiple images that you have to maintain it's just one sprite sheet that you're gonna be loading up And there's tools that we interact with so now that you actually have things on the screen We want to interact with it in some way now of course you're gonna be running a game loop so you can actually have Elements falling or moving around or shooting a bullet that that bullet has its own like logic that's running But if you want to perform tasks on these sprites and we call them nodes So each node there you might want to tap on something or perform an action and actually be multiple things An action could be a sequence. Maybe it's a spawn sequence. Maybe it's a rotation Maybe it's an animation and you're running these actions in parallel You can run them asynchronously because we async a waitified everything so if you want to wait on one thing to occur Or we have a way of just streaming up a bunch of actions in a row So for instance, maybe you tap on the screen We want to move a monkey to that new coordinate You want him to kind of animate over there and then you want him to stop animating when he's done So it'd be three specific actions that you queue up in a row Which is pretty nice Now the touch events which we're actually going to interact with as well There's multiple events that we've given to you as developers to interact with Normally, it's just kind of one touch at a time. So if you're just tapping on the screen You can see when all touches are occurring you can Assign a touch to a specific node so say when that that monkey was tapped or when that banana was tapped and get a specific touch event for that And you get multiple events like when the touch begins so when I actually touch down If I move and you'll get coordinates across the way and then when I actually pick up off of the screen So we enable you to get all of those and what we're doing is we're handling all that business logic For you automatically of you having to manually register for the different events on iOS Android and of course windows And what's really great is we even enabled multi-touch on there as well So if you have a game then it requires multi-touch and your device supports multi-touch like almost all of them do now You can actually have multiple coordinates and see if people are touching the screen in multiple locations So if you're building out that type of game totally doable But there's a whole lot more that we're actually going to try to cover and kind of take a look at and you'll see a little bit of this In the actual game so we have a cross-platform accelerometer So you can actually see if the device is actually moving and rotating We have a cross-platform audio to play background play sound effects do a lot of things We have primitives. So if you don't want to do any Actual artwork in your game you can do a hundred percent is drawing lines and primitive graphics and the scales across all the different Platforms, so if you're gonna do like a geometry Wars clone for instance, you could totally do that With advanced particle systems so you can spawn particle systems and those are caked in we have a few for you automatically You can pull in your own particle systems and just spawn them off and and have them do whatever you want So for instance, you could just keep spawning a background image That's just doing crazy stuff and firing off or rotating over and over again We have parallax as you see as the monkey moves up and down the actual clouds are moving with it So you can say I would like that That as this monkey moves keep it to be a specific I'll go back and forth so you can see it again I want to parallax those actual clouds you kind of link those two objects together to move them around which is really cool It's handled all for you automatically We also have multiple effects so we're doing like wavy type things and spinning things Automatically and we'll see that we're actually gonna set up some some effects actually spin this banana and move this ball around and move it around on the screen and Finally, we have an a very advanced physics system In the actual game logic as well So this actual box that's kind of moving it'll actually handle the physics of gravity for you automatically kind of in a world space And all of these little samples that you're seeing here are all available Actually on our github repo because everything is open source We actually have one huge huge solution that has like every single sample in use case So it's like if you want to try to do something we have a test for it So that's kind of the core of cocoa sharp. There's a lot in it It's a lot of text kind of on a slide But what I wanted to do is just kind of build out a game and take a look So when you get started and I'm gonna do everything inside a visual studio You can do everything also inside of I mean I'm gonna do it in Xamarin studio You can also do everything inside a visual studio and I'm just gonna focus on iOS and Android But what you'll see in the actual business logic of the iOS and Android application as though there's three lines of code So if you want to then bring your application to a new platform all you have to do is copy the image assets in Add three lines of code and you have a game. It's just pretty awesome So we kind of start and I like to start everything actually on Github so this is the github and I'll zoom in here for the github. It's github.com Mono Cocoa sharp now we put this on the mono Mono directory because it's kind of built on mono game. It's open source and actively people can contribute to it Especially if they're on the mono project and that's kind of what we're actually doing You know Xamarin where the creators and the maintainers of the mono project Which is an open the original open source implementation a net and that's what we're actually leveraging here on iOS and Android to bring That over to create iOS and Android apps. I'm gonna pull this down since this doesn't fall There we go. Cool. So if you want to see what we're doing and have issues or how to pull requests or look at the wiki Everything is here So every single bit of the entire cocoa sharp library is actively here Including you could load this up on Android Mac OS. So we have Ooya support And then all different types of windows and windows 8 and phone support as well You can see what we're doing in here. We have a forum So if you want to actually go and actually maintain to it actually have questions We're active there and we have this wiki and everything is under MIT license, which is really really great On the wiki it kind of gives you everything how to get started More links to the forums so we have a slack channel And we even tell you if you want to deep-dive on why we created it what the core differences are and getting active With the new get packages and look at what we're doing in the future as we evolve cocoa sharp everything is going to be documented here Now what we did is we created some project templates So I like to go over here and you can see there's project templates in our wiki and The project templates all it does is tell you how to get started in other visual studio or Xamarin Studio In visual studio all you do is just kind of download a project templates of the vsix file Into the extensions and that kind of gets you good to go over there Now if you're inside of Xamarin Studio Xamarin Studio is our IDE. It's built on it works on both Windows and Mac It's a lovely lovely ID eats very similar We built it with the visual studio developer in mind So all the key bindings are the same across the board and it looks very much like an IDE We have built-in source code analysis in it if you ever use Xamarin Studio at all we have this thing called an add-in manager very much like visual studios add-in manager But when you go over to gallery, there's a cocoa sharp entry and all you do is install the project templates And that'll kind of get you up and running to do iOS Android and Mac development for cocoa sharp and that's really convenient So once you have that installed to get started with cocoa sharp you're gonna set new solution and under app we're gonna see Some brand new templates in here this cocoa sharp showcase and Empty project so we're just gonna kind of do file new project see what that looks like so we'll say And DC cocoa sharp there we go Now we distribute everything through new get so Everything is just a new get package and on up top here. It's gonna add a bunch of packages for me automatically Over and over and over again. We'll probably even update them. Let's take a look here That's good. Let's see what we're on 140 perfect This is actually pretty nice it's gonna okay got everything awesome So inside of here what we have is an Android project an iOS project and a common Lib and that common portable class library is gonna handle all of our game logic of our application And we can just see that there's just a cocoa sharp PCL inside of here So the main app delegate that's here. This is gonna handle kind of our business logic starting our application So we can see if the application finished loading and inside of here It sets up kind of some default state for us to say hey, I have a content folder. There may be some animations fonts sounds I really like this to be 1024 by 768, but it'll handle the scaling for you automatically on these different devices If you're an Android developer at all, you know, it's a crazy crazy beautiful disaster over there of a bajillion devices and we handle all that for you so scaling or putting borders around your game for you automatically and Then here we'll actually handle a high density or low density images And then it starts and creates a game layer as that child and it runs with that scene So that's the start and we can see we even have a little pause state inside of this that you could check at any time Then we have a game layer so this game layer is going to do a few things It's going to load up some sprites and if we look actually inside of this content directory Over an iOS. We'll see some images in here HD and Lodi. It's just a monkey. That's inside of here. There he is adorable So it's gonna load up a few monkeys add it to this layer. So this this is a layer a CC Great layer gradient that's in here So that every single one of your screens will be some sort of layer a solid color you can add a background or this one's gonna be a gradient We're gonna create some of these CC actions and the default there. We have some move by we're gonna create a sequence of Repeating it and we're gonna spawn this this this effect this wave effect So I'm just gonna kind of run this so we can kind of see what it looks like I'm gonna select an iPhone 5s simulator Let's see what this looks like. Oh Go ahead and spin up there So something too too crazy It just kind of shows you some things that you can do so loading some sprites putting them on the screen and kind of moving them around We're also generating a particle system in the background and what I can see is if I want to interact with this I can tap on the screen and that particle system moves So I want to kind of show off the core of what we're gonna be doing when we actually build out a game So up here what we'll see Or is it that down here? Run with there it is We have this event so we create an event which is a listener to say whenever anyone touches anywhere on the screen I could subscribe to multiple touch events So there's on began on cancelled on ended on moved and whatever I lift up here We go in and we run this touches ends and what we can see that all I'm doing is this galaxy system That's creating all these particles. I grabbed the first touch event grab the location if it was moving I stop it and I say let's move it somewhere else on the screen And then I told to move over a certain amount of time so over this duration move it to this position So really really simple and I run this action. So everything's action based inside of here. It is really nice So that's what is going on here. So if I move this around we can see it just kind of moving around on the screen Now if I load this up over on Android we can take a look over here. So let me go and pump up an Android simulator real quick Now I'm going to be using our Xamarin Android player, which is a free Emulator from Xamarin anyone can use it for any type of development on Mac or PC It's all based on virtual box. I was really really fast Which is really really nice. Let's see if this will spin up here. So we'll go ahead and pop it over here Just make sure I have everything set up correctly in my build actions. Nope No got an email perfect like that. There we go And What we see is actually in the iOS or Android application We have our our actual iOS app delegate and this is kind of the start of the application So I'm creating that shared app delegate and then over in I got to rotate it. There we go Then we do the same thing on Android, but the same exact game same image assets everything like that now over on Android I didn't actually write any Android code. I just everything in C sharp Because I'm using Cocoa sharp So if I look at Android over here, we can see that I'm using some packages Cocoa sharp Android in my main activity What do you know? I just have a little bit of configuration and I start the game That's it. So even if you don't know Android or iOS development at all You don't really need to because you're just learning the Cocoa sharp API and it's just going to run over there now if you want to tie in for instance into like Google Play Games or the the iOS equivalent for what it's called game center game center. That sounds right You can actually add that integration if you want to put on Amazon devices You could download the Amazon SDK and put it right in there and the same thing would be true over on Windows You would just pull in the Cocoa sharp Windows new get package add these same lines of code And then you could integrate with anything you want if you want to integrate in Cortana or something like that You absolutely could because you could just bring in those packages So let's actually open up another game and actually build some things out from scratch and walk through kind of building out a game and The kind of sample you saw with that little little monkey kind of moving around and collecting bananas That's a game that we have called gone bananas. Go figure. We're big fans of monkeys as you can see this is Sebastian He's up here hanging out with us today and and this gone bananas is available for every single platform Windows iOS and Android and I've just opened up the iOS and Android bit here and what we're gonna notice is that we still have an Assets folder and we have content inside of here. I added some custom fonts into my application Some sounds I have an mp3 and I have some wave files And then I have some I've an animation so if I open up this monkey here. This is that same thing We've even stuck the banana in there so we can pull it out just a sprite sheet And then I have this p a p-list which was generated by a tool for me if I open this up think Here let's try to open it There we go, it's just keyframes so it says what frame do you want to use per so it's just like a Spits that out for us and then we have the balls There's the balls that we're gonna be dropping from the sky and the idea is that these bananas and balls are gonna fall from the sky We want to collect the bananas if the if the actual balls hit us then the game's over But we also have this background that we're gonna use grass and that'll be kind of nice all right perfect So that's everything that's inside of my core application if we look at the app delegate again on iOS and Android is gonna be the same Except we're gonna create this gone bananas app delegate And that's what's gonna start here, and I want to write a few hundred lines from scratch So some of this code is in place some of it we're gonna write out, but it's gonna look very much the same We can see that I'm preloading some effects sound slash taps sound slash background music I'm just grabbing the main window size and setting that as the default and I'm running the game and we're running this start layer So there's three Three screens to our game. There's a start screen a main game loop that's gonna run all of our business logic and an end game And we can loop those back and forth so the start of our game here If I open up the main game layer It's pretty pretty simple I have a click handler and all it does is whenever I touch anywhere on the screen it'll start the game It's gonna start my main game layer, and I just add that touch listener I have a black background and all I do is put a label we create a CC label and you can by default Just slap down some text load up custom Fonts and we can see I have aerial in here because I brought an aerial you set Specific size and then you say where you want to do it. I'm just gonna align it in the center and add that in That's it The the main game layer that we're gonna interact with here is gonna do a bunch of stuff and we're gonna kind of walk through this later But when this is over what we're gonna have is a game over layer and these two are kind of already caked and all this one Does is it looks very similar to our first game, but it takes in a score so I could display a score on the screen on there and I have a score label and a play again label whenever they tap it It just brings them back to the previous screen to start a new game. So very very simple inside of here That's it. All right So let's go into our main game layer and that's where all of our actions gonna be and I set up all my variables ahead of time So how fast do I want the monkey to move? How long does my game last which is gonna last 60 seconds and how many maximum amount of balls do I want on the screen? We have some physics. I'm not gonna go into physics logic. So I'm not a physics major I just kind of put that in a helper class, but we have a bunch of physics that's gonna happen And then we can see what else I'm doing here So first I'm gonna set up what I want to track in my game. So the elapsed time so after 60 seconds I should end my game that's gonna be a goal. I'm a main monkey and I'm gonna interact with so it's gonna be a main Sprite, he's just gonna move around and animate on the screen And then what I'm gonna do is I'm gonna keep a list of visible bananas and hit bananas So the idea here is that I can just kind of track and actually see the rotation and check the collision of any visible Bananas on the screen and then the hit bananas will go into a list that I could track later that I've removed from the scene and I can Get a count for them later on And then I'm gonna kind of start setting up my game and some of these are just Animations that'll be played and we'll set up so we have a walk animation and we'll set that up in a bit a walk repeat So it'll loop forever Animation and a way of stopping it. So this just says hey on any node stop all actions That's a little extension method so you have a way of saying if you're moving stop right now Just stop walking stop doing whatever you're doing. I Also have a sprite just like we had our monkey. I have a grass. That's gonna be our background sprite. I Have a rotation so I say over point eight seconds rotate the banana 360 degrees we handle all that logic for you don't have to worry about it We'll take your sprite and rotate it just with this action and Then what we do is we I set up a little action just like we said stop all actions when I want to stop walking Also remove this banana when it's done. So I'll remove the node that banana node from the parent So I'll just remove it from the game scene if I collide with it And that'll be any any node and then I set up my balls that are here in my physical world So I have a little bit of logic just initializing And we're gonna walk through adding some things side by side But I just wanted to do a little bit of setup here So I might a touch listener touch all at once and that's whenever I end and pick it up I set my background color to white initialize my list and in this instance I'm gonna create that sprite batch so those balls were in a batch file basically inside of there I'm just gonna pull down that texture and split it all up. So I have my balls ready And I'm gonna add grass add monkey and start scheduling these are all blanks We want to fill those in Now all we need to do at this point is actually start adding some things to the screen So let's go ahead and do a few things It's out here. Let's remove our monkey so we can kind of add one thing at a time so we're gonna add our grass and Our grass is gonna equal a new cc sprite And it's gonna take in a file name. That's gonna be grass There we go I should say let me just run this first. You can see what it looks like. I think this should run Let's take a look So I've really done anything just initialization and hopefully if I didn't crash anything it shouldn't do anything Perfect there's Tap screen to go bananas and it's white. So that's what we have that's our game right now We're gonna transform this over the next 20 minutes So we're gonna add some grass So first we'll do is we'll initialize that grass and then we'll say add child to this layer and there's grass now I do get some events. I get my application event lifecycle But each layer also has some event life cycles as overrides And we're gonna see whenever this was added to the scene whenever this this layer was added to the scene I can initialize a few things So I'm gonna go ahead and position my grass right in the middle so we can run this again So I create my grass add my child and run it again Grass that just happened two lines of code not bad. Let's put our let's put our monkey down on the screen So here what I'm gonna do is this is a little bit more logic So I didn't want to write this from scratch, but I'll walk through it with you So instead of creating a sprite this monkey is a sprite sheet with all those animations So what I have to do I'm gonna pull this down a little bit. We have a little inline function here Let's say load up that animation Peelist info that sprite sheet that's gonna have the different keyframes and Grab all the animations that start with frame because frame one frame two frame three frame four for each animation And then I'm gonna initialize a few things I'm gonna animate and create an animation So I take all those keyframes and I say each frame should last point one second. So we kind of moves across the screen That's the main animation and then I'm gonna create a repeat So just keep animating that walk animation over and over again So there's an animation and I tell the action is to animate the animation and repeat that forever So just kind of always keep doing that and the last thing that we're gonna do is create my monkey Based on just the very first sprite in that list give it a monkey name and And then I scale it point two five percent. It's a really big monkey and then I add that monkey to the list That's it. So we have our animation that's gonna be looping over and over again, and I create my monkey So now I can also put the monkey right in the middle load it up again Now we got a monkey But now of course we want to do something with the monkey because if I tap on the screen I'm actively tapping nothing's happening So let's move them around a little bit So we saw earlier if we look up top I created this on touches end Here it is. It's blank. I have it down there in case of emergency. We're gonna try to type this out So every time I move we want to move that monkey to a new location and we want to animate him So the first thing that we're gonna do is I'm gonna say a monkey Let's stop all of your actions. So if you're moving if you're doing anything if you're rotating for some reason stop doing that and calm down Then we're gonna grab our location and that's gonna see we have a touch event So you can see which touch event came in but also a list of CC touches So we'll grab our first touches out of that list and we'll grab the location on screen so the physical location on screen and Then we what we need to do here is we need to take that location And we're gonna call a method called world to screen space And what that's gonna do is it's kind of take the current location on the screen and put it into our world that we have here So I can manipulate it Now we have to do a few things We don't want all of a sudden to say move that monkey over there in point one second and he goes racing across really fast Or maybe he's only moving just like a few pixels So we need to calculate how fast this monkey should move. So what we'll do is we'll create The distance we'll calculate the distance here. So let's say distance And we have some helper methods Inside of here on our point. So there's a point x and y coordinate. There's a distance We can calculate the distance between two points. That's pretty pretty lucky So we'll say monkey position where you're at right now to our location and we're good to go And then we'll say speed There and we'll take our distance and we're gonna divide that by our monkey speed So it's gonna kind of normalize our speed over distance ratio I may not be the best math in the world, but it works for the sample Now we need to actually go ahead and actually Move our monkey. So we have some moves in here. We can move by move by state move to move to a state So we're gonna move to over we'll do it over that Speed all right. Sorry. This is calculating the time. That's my fault. Sorry time The distance divided by speed that equals time. Yeah, I think calc math. It's been a long time So one thing I forgot about game development when I was starting all this like oh math And we're gonna move that to the new location there we go And we're gonna tell our monkey a few things we're gonna tell our monkey to start animating We're gonna tell it to move and then at the end of it. We're gonna tell it to stop animating So we'll first say run action monkey. Oops. That's not it say move to there we go monkey dot start Where'd it goes run action? There we go run action. There's run action async run multiple actions And we're gonna tell it to walk repeat Then we'll say monkey dot run actions So we're gonna run two in a row so this will pair them up so it'll always walk repeatedly And then we're gonna run some other actions So we're gonna say move to first and then we have a walk animation stop So we're kind of chaining up multiple events. We're gonna say hey run this first animation and keep running it That's our animation that's looping over and over and over again for him walking and animating and then run the action to move to that location And then what this does is when we're done it'll run the next action Automatically, but we're running them in two separate calls. So it's always walking and repeating over and over again So let's run that again There's our monkey I'm gonna tap up top boom if I zoom in you can see I'm moving around pretty adorable pretty adorable alright And that's your game done. I'm just kidding. Alright, so what do we need to do next? So the next thing that we need to do is actually schedule some things So we've added our grass added a monkey and then we need to schedule some things and what does that mean? Well every game that you ever create has a main game loop the game loop is always running every single frame and you need to update it So what we did is we enabled a way of Scheduling things and you can schedule it's kind of like a task-out run almost What we said is keep running this task at certain intervals in time and you can schedule up a must a bunch of actions over and over and over again So if I come down here After this we have this start scheduling and I kind of lined up what I need to do So the first thing I need to do here and I start scheduling is I need to schedule a banana I need to schedule to add a banana to the visible bananas list I'm gonna add some time to our game loop. So I'm tracking up to that 60 seconds Over and over again. I'm gonna check to see if the game is over and then go from there So down here. I created a few helper methods. We have is time up pretty simple math is the elapsed time greater than the game duration And then I also had this add banana thing and what this is gonna do It's gonna load up our sprite sheet take out the banana That's in there. It's gonna use a helper method to start in a random position So I have a bunch of helper methods that I'm not gonna go into it's a bunch of more math and logic and shenanigans So we'll just pretend that I went over that. It's all the other stuff's online We're gonna grab a random position We're gonna start and here's the banana on the screen at it and then similarly like how we're removing the Actual monkey on the screen. We're gonna do the same thing. We're gonna move the banana down to the bottom of the screen and Then when it's moved to the bottom of the screen remove it from the view and then always rotate that banana over and over again So you're gonna see it spinning on the screen So let's just do that. We'll come up to our schedule and we have a keyword called schedule Now don't be worried. We're gonna actually put some inline actions inside of here. We're gonna use this one It's gonna take a float which is the time that it was took in between the last call and an interval So we'll say t and we'll just put a little stuff in here and we'll do auto 1.0 f So every second we want to add a new banana to the game. So we'll say Visible bananas dot add we're gonna call that add banana there we go We're gonna say our elapsed time plus equals t That's a time difference between there. So one second two second three second But it's it's always one second so we could change this and this could be variable then what we're gonna do is say if is Time up Let's end game Now this end game method is down here and I haven't done anything We're gonna do that later But our core business logic is in place and we can start it up again and just see what the scheduler is gonna do over and over and over again And for lucky a whole bunch of bananas should fall from the sky Boop And that's pretty cool so far and just doing it all randomly so I'm kind of getting a random x in there. That's pretty cool Alright, so let's do another thing. Let's go ahead and schedule another one so we'll say t and we'll come in and we'll add a ball now In here, we'll do that every I'm gonna do every point one five seconds because it's pretty hard with point one and I have a add ball method and That of course is gonna be over in my little helpers class and what this is gonna do is it's only gonna add the ball if If we have don't have our maximum amount of balls in there So the balls are already is gonna kind of be there. It does a whole bunch of 2d vector stuff to kind of like bounce around so it bounces up and down off the physical world space That's created more math. Don't worry about it Games don't worry about math. It's not important. So we'll run that again. It's actually very important learn learn lots of math Cool so we'll tap again and now what we'll see is now we have a ball But the balls are just kind of the top of the screen because we haven't animated it at all yet. So we need to do that But they're there. They're they're occurring. That's interesting Boom. All right. We're almost there. All right, we could do a few things. We could schedule Every second to do something. We'll say hey just every or every frame. It could take in just an action We'll say check collision inside of here now check collision Not to do T Just do this to make it easier on myself. There we go now check collision There we go is another helper method that I wrote but it's actually really not too difficult What we have inside of here is bounding box transformations now Bounding box might not be the best because you know, it's a monkey with a banana But you could actually we have actual documentation of how to actually put multiple bounding boxes around that monkey So you might want to do the banana than the actual body inside of there But for simplicity's sake I'm saying for each visible banana in the list If the bounding box that's transformed around it enters interacts with the monkey then that banana is hit We're gonna play a sound effect called sound slash tap. We're gonna explode that banana We actually do a particle and we'll look at what that looks like and we're gonna remove it from the parent And this explode thing here is Is actually a particle explosion. We have a bunch of them caked in automatically But this one just says create and do an explosion of particles create 10 of them and automatically remove it from the scene when you're done And that's it But really just doing collision just looking at the core collision that you have like that's pretty simple math Just every frame loop through my visible bananas and be good to go now, of course kind of best practice If you have a thousand bananas on the screen, that's gonna be pretty difficult to check every frame So you might want to actually do some more logic inside of there with like link or something because links super fast, right? so So we'll check collision and we'll still see that these balls are up top that's that's cool But we should this gonna like make the game really really great Because we're only just checking the bananas. I'm just gonna win forever So let's see here. So now if I actually tap here, boop It makes the game easy to win Perfect so now we got some collision going on we got some particles happening, which is a few lines of code Now we need to schedule a few other things this one's a little bit more difficult Because what we're gonna do is we're gonna loop through all of these balls on the screen We're gonna do the same kind of detection to see if I actually am inside the physics body of the specific ball And if so, I'm gonna remove that sprite and then we're gonna actually move around on the screen that's a little bit more complicated But this is gonna move the ball and destroy the balls when they're not moving So it's a little bit more math inside of there. So let's take a look here So now our balls are falling And that's bad. Let's see if I put any collision not no collision yet. Cool Perfect So now what we need to do is actually Do a few more things which is end our game. So we have our balls We have our things occurring inside the game and then we can actually end our game if we hit a hit a physical But or a ball on the screen so unschedule everything unschedule all We're gonna create a new layer. We'll call the our game over Scene with score. We're gonna pass in a main window There's our window and we'll take in our score and that's gonna be our Bananas no, it's called hit bananas dot count. So how many we hit and then what we can do is say What do we have to do it's the director thing Sometimes I just forget core logic window. Default director there we go window dot default director dot scene Replace scenes or it's gonna move and we're gonna move to this new layer or scene if you will and that's actually should be seen There we go. Perfect. And there we go So that should be good to go now I need to double check to see why my check collision is here Hit bananas ball count good to go balls hit count end game great. All right. Ah So check collision it does a little bit more than that it adds in it Hits updates the hit and then it also does some detection here on the the bounding box of the actual Balls that are on the scene. So now we can move this again Now Ideally I should be able to kind of get some here and boom I hit the ball that's interacted with it and I got two bananas if I tap again. I start up a new game And we're kind of good to go now. It's kind of boring because there's no background music That'll really spice this up. I'm pretty sure so let's go ahead and add that So I'm gonna come up to the top Way up top here into my game layer. I'm in the right scene. There we go And after we set and start scheduling we're gonna use this thing called cc simple audio engine We're gonna use the shared audio engine. So anything that's kind of all the sound effects and our game music That's there. We can adjust the volume. We can also play background music And if we take a look inside of here, what we'll see in our content sounds We have background music. So we'll say sound slash background Music mp3 and we'll loop that why not Boom thousand times better basically best game ever. It's pretty cool. Not even moving I'll see how you just gonna dodge all the balls. That's a pretty awesome monkey right now. I'm just like, oh, there we go Pretty good music, I think we've all that for like a dollar All right, so what's really cool now is that I can now just set Android as my startup project here and Actually select that same Android device the emulator that I had there. Let's rotate it back. This is a portrait game The same exact game same particle physics everything is reused a hundred percent across the board and this is scaled down Obviously so I sick ale this up, but it's a full another game on Android No additional work that I needed to do because everything is in my core Portable class library with Cocoa sharp, which is pretty mind-blowing to me overall And you kind of have a full game with just a few lines of code some math and some physics But besides that it's all there. We can all figure out physics. It's easy, right? well Now all of this source code and more is all available online and I'll have these slides on my slide share But you can just always tweet at me, but it's also all on that Cocoa. So when you go to the mono slash Cocoa's Sharp repo on the mono GitHub. There's also the sharp Cocoa sharp samples and there's tons of games in there Not only just this one, but there's an extension to gone bananas that actually has Apple watch support So you can control the monkey from the watch, which is really crazy Which is really cool. It's actually published. It's called gone bananas. It's on iOS and Android by one of our evangelists a previous evangelist Mike Bluestine because he created this sample, which is awesome a whole huge walkthrough of creating the step-by-step I didn't even go into the parallax or creating the Sun with the spirals and the background everything like that There's a lot of cool additional stuff that you can create. I'll let everyone take a second there to take these snapshots I'll tweet these out later as well. I'll have the slides online If you follow me on Twitter Now a lot of people ask about pricing like what's free. What's not free? Well Cocoa sharp is open source And if you want to create Windows applications, you don't pay us for that You pay Microsoft for whatever you want to do if you want to create games with Xamarin and Cocoa sharp Does cost some money some subscription to do that We have an indie edition starting at $25 a month for iOS and Android to publish your game But to do it on Windows or anywhere over there, you're good to go once you have your Android or iOS application You're paying us for the compiling the technology to bring that over and build the iOS and Android app We haven't gotten sorry with Xamarin just playing around with it. You go to Xamarin.com. We have a full 30-day trial I did mention Xamarin University earlier. We have tons of great stuff. I have some Xamarin stickers and plural site stuff up here There's tons of great resources including our documentation to get started, but we just yesterday I'm super excited that we did it yesterday and not tomorrow But we have a full 30-day trial of Xamarin University Which is live interactive and structures that go online and a go-to meeting and you learn 24 hours a day seven days a week And it's like you're like a student you become a fully Xamarin certified developer The free trial gives you a few courses and access all of our guest lecturers I was just listening to Miguel decaza our CTO. I one of our guest lecturers talk about just the state of net is really intriguing I did that before I went to bed Which is really cool So I recommend everyone checking out Xamarin.com University because it's free to do a free trial and there's no credit card or anything that you need Of course, you can find me at any time just at James at Xamarin.com and at James Monta Magno nice short and sweet Twitter handle And I have a Xamarin form session in this room at 1620 the very last session the day. Yeah, so with that Thank you guys so much I'll be around as well up here if you guys have more questions. I have some plural site stuff and stickers as well
creating just mobile applications themselves and even Linux that powers tons of amazing games you use Xamarin to actually create your native development a few has anyone done Windows And there's multiple sprites that you can have for the different events on iOS Android and of game so we have a cross-platform accelerometer So can pull in your own particle systems and just system In the actual game logic as well So this So every single bit of the entire cocoa sharp code. I just everything in C sharp Because I'm and I'm gonna interact with so it's gonna be a to animate the animation and repeat that forever move and then at the end of it. We're gonna tell documentation of how to actually put multiple