Presentation is loading. Please wait.

Presentation is loading. Please wait.

Gameprogrammeren: Menu’s in Penguin Pairs

Similar presentations


Presentation on theme: "Gameprogrammeren: Menu’s in Penguin Pairs"— Presentation transcript:

1 Gameprogrammeren: Menu’s in Penguin Pairs
Arjan Egges Paul Bergervoet Wouter van Toll

2 Overzicht

3 Menu’s Tekst Aan/uit knop Slider

4 TextGameObject class TextGameObject : GameObject {
protected SpriteFont spriteFont; protected Color color; protected string text; public TextGameObject(string assetname, int layer = 0, string id = "") : base(layer, id) spriteFont = PenguinPairs.AssetManager.Content.Load<SpriteFont>(assetname); color = Color.White; } ...

5 TextGameObject public override void Draw(GameTime gameTime, SpriteBatch s) { if (visible) spriteBatch.DrawString(spriteFont, text, this.GlobalPosition, color); } public Color Color get { return color; } set { color = value; } public string Text get { return text; } set { text = value; }

6 Knoppen: muisklik detecteren
Hoe weten we of op een knop geklikt wordt? Muisknop ingedrukt Muis is binnen de rechthoek die de knop omsluit BoundingBox property toevoegen aan SpriteGameObject: public override Rectangle BoundingBox { get int left = (int)(GlobalPosition.X - origin.X); int top = (int)(GlobalPosition.Y - origin.Y); return new Rectangle(left, top, Width, Height); }

7 Eenvoudige knop class Button : SpriteGameObject {
protected bool pressed; public Button(string imageAsset, int layer = 0, string id = "") : base(imageAsset, layer, id) { } public override void HandleInput(InputHelper inputHelper) { pressed = inputHelper.MouseLeftButtonPressed() && BoundingBox.Contains((int)inputHelper.MousePosition.X, (int)inputHelper.MousePosition.Y); } public bool Pressed { get { return pressed; } }

8 Aan/uit knop (toggle) class OnOffButton : SpriteGameObject {
public OnOffButton(string imageAsset, int layer = 0, string id = "") : base(imageAsset, layer, id, 0) } public override void HandleInput(InputHelper inputHelper) if (inputHelper.MouseLeftButtonPressed() && BoundingBox.Contains((int)inputHelper.MousePosition.X, (int)inputHelper.MousePosition.Y)) sheetIndex = (sheetIndex + 1) % 2; ...

9 Aan/uit knop (toggle) public bool On { get
return sprite.SheetIndex == 1; } set if (value) sprite.SheetIndex = 1; else sprite.SheetIndex = 0;

10 Slider class Slider : GameObjectList {
protected SpriteGameObject back, front; protected bool dragging; protected int leftmargin, rightmargin; public Slider(string sliderback, string sliderfront, int layer = 0, string id = "") : base(layer, id) } ...

11 Slider public Slider(string sliderback, string sliderfront, int layer = 0, string id = "") : base(layer, id) { leftmargin = 5; rightmargin = 7; back = new SpriteGameObject(sliderback, 0); this.Add(back); front = new SpriteGameObject(sliderfront, 1); front.Position = new Vector2(leftmargin, 8); this.Add(front); dragging = false; }

12 Slider public override void HandleInput(InputHelper inputHelper) {
if (inputHelper.MouseLeftButtonDown()) { if (back.BoundingBox.Contains((int)inputHelper.MousePosition.X, (int)inputHelper.MousePosition.Y) || dragging) float newxpos = MathHelper.Clamp(inputHelper.MousePosition.X - back.GlobalPosition.X - front.Width / 2, back.Position.X + leftmargin, back.Position.X + back.Width - front.Width - rightmargin); front.Position = new Vector2(newxpos, front.Position.Y); dragging = true; } else dragging = false;

13 Slider public float Value { get
return (front.Position.X - back.Position.X - leftmargin) / (back.Width - leftmargin - rightmargin - front.Width); } set float newxpos = value * (back.Width - leftmargin - rightmargin – front.Width) + back.Position.X + leftmargin; front.Position = new Vector2(newxpos, front.Position.Y);


Download ppt "Gameprogrammeren: Menu’s in Penguin Pairs"

Similar presentations


Ads by Google