This line tells the app that we need this timer in this app so allows us to make a new instance of the class.ĭispatcherTime.Tick += dispatcherTimer_Tick this line is telling the program that the main timer will have this dispatcherTime_Tick event to run with each tick. We are adding the following lines to this function, this is the function that runs first when the app will load so we want to initialize the timer when the app loads so the activities and instruction will be ready soon as its loaded on the screen.ĭispatherTimer dispatcherTime = new DispatcherTimer() this line is accessing the timer class from the Threading we added earlier. Inside the MainWindow() function we will declare the timer and initialize it. Both of these variables will be used in the key down and up function to help us move the character left and right. By default, it will be set to false when we declare it unless we set to true. Both go left and right are Boolean type variables they only have two options, it can either be true or false. Int dropSpeed = 10 another integer which holds 10, this integer will be used to simulate gravity for this app.īool goLeft, goRight this is a short-handed way to declare variables. Int speed = 10 this is an integer variable which holds the number 10 inside of it. Now before the public MainWindow() line we need to add our global variables, these variables can be accessed from any function in this program this is why we call it GLOBAL. By adding this line we will have access to the timer properties. We need the timer to use as the main engine for the app since there will be objects moving and colliding with each other we need a timer to keep track of all of these activities. On top of screen, under the line using we need to add the lineīy default, WPF forms app will not include the timer, but we can add it ourselves to use it in this app.
Open canvas 6 key bindings code#
Now right click on the Canvas_KeyUp and Click on Go to definition.Īs you can see both of the events are added to the code now. Visual studio will open the C# script file that’s linked to this app and then add the Canvas_KeyDown event. Right click and then click on Go to Definition. By clicking on this option Visual Studio will add the event got us. Right click on the Canvas_KeyDown and you will see the option Go to Definition. Lastly we end the canvas as we will not need to add anything else to it. We also have a TAG property in them, this is important because we will use this to detect the collision between these and the rec1. Each of them has their own names, height, width and location. I you look inside these tags you will notice they all have similar properties inside the rectangle tag. We can set it to where we want it so in this case, we want this rectangle to be 307 pixels from the left and 51 pixels from the top.Ībove the are 3 platforms for this app. We give its width and height of 50, when we declare a number inside of this it calculates in pixels so this rectangle will be 50 pixels wide and 50 pixels high.
We need to give it a name and, in this case, its rec1. This is the first rectangle inside the tag we are giving this rectangle some properties.
We will use them to see which platform the player character lands on. These two lines of code above will add two labels to the screen. This is the open canvas code, inside this tag we have several options included such as key down and key up events, whether this canvas is focusable when its loaded, margin, horizontal alignment and width. Title=”MainWindow” Height=”600″ Width=”525″ FocusManager.FocusedElement=”“>įirst inside the title put the code above, this code will focus right on the Canvas when the app is loaded, this part of code will allow the program to respond key down and key up event.