Du bist nicht angemeldet.

Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!

Werbeanzeige

11

02.11.2018, 11:42

Es funktioniert nicht, die Balken werden nicht an die richtige Position gezeichnet. Ich sehe die beiden Balken garnicht auf meinem Tablet da sie viel zu hoch und viel zu tief gezeichnet werden. Außerdem weiss ich nicht ob die Berechnungen für die Höhe des Balkens richtig sind. Ich habe keine Ahnung was ich falsch mache ?(
Weiss jemand warum die Balken nicht an der korrekten Position gezeichnet werden?

Gesamter Code der beiden Klassen Camera und Game1:

C#-Quelltext

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
 public class Game1 : Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        Camera camera;
        Vector2 vp, gameWorldSize = new Vector2(1334, 750); 
        float ScaleX, ScaleY, Scale;
        Vector2 Newcameraposition;
        Texture2D Hintergrund, Balken;
        float BalkenPosOben, BalkenPosUnten, BalkenBreite, BalkenHoehe;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            graphics.IsFullScreen = true;
            graphics.SupportedOrientations = DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight;
        }

        protected override void Initialize()
        {
            base.Initialize();
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);        
            Hintergrund = Content.Load<Texture2D>("Hintergrundbild");
            Balken = Content.Load<Texture2D>("Balkenschwarz");
            vp = new Vector2(GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);
            ScaleX = vp.X / gameWorldSize.X;
            ScaleY = vp.Y / gameWorldSize.Y;
            Scale = Math.Min(ScaleX, ScaleY);
            camera = new Camera(new Vector2(gameWorldSize.X / 2, gameWorldSize.Y / 2), Scale, vp);

            float ScreenHoehe = GraphicsDevice.PresentationParameters.BackBufferHeight;
            float Bildhoehe = Scale * vp.Y / 2;
            float Differenz = ScreenHoehe - Bildhoehe;
            BalkenBreite = GraphicsDevice.PresentationParameters.BackBufferWidth;
            BalkenHoehe = Differenz / 2;
            BalkenPosOben = (int)gameWorldSize.Y / 2 - Bildhoehe / 2 - BalkenHoehe / 2;
            BalkenPosUnten = (int)gameWorldSize.Y / 2 + Bildhoehe / 2 + BalkenHoehe / 2;   
        }

        protected override void Update(GameTime gameTime)
        {
            Newcameraposition = new Vector2(gameWorldSize.X / 2, gameWorldSize.Y / 2);
            camera.Update(gameTime, Newcameraposition, Scale, vp);

            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend, null, null, null, null, camera.GetMatrix());
            spriteBatch.Draw(Hintergrund, new Rectangle((int)gameWorldSize.X / 2, (int)gameWorldSize.Y / 2, Hintergrund.Width, Hintergrund.Height), null, Color.White, 0, new Vector2(Hintergrund.Width / 2, Hintergrund.Height / 2), SpriteEffects.None, 0.10f);
            spriteBatch.Draw(Balken, new Rectangle((int)gameWorldSize.X / 2, (int)BalkenPosOben, (int)BalkenBreite, (int)BalkenHoehe), null, Color.White, 0, new Vector2(Balken.Width / 2, Balken.Height / 2), SpriteEffects.None, 0.09f);
            spriteBatch.Draw(Balken, new Rectangle((int)gameWorldSize.X / 2, (int)BalkenPosUnten, (int)BalkenBreite, (int)BalkenHoehe), null, Color.White, 0, new Vector2(Balken.Width / 2, Balken.Height / 2), SpriteEffects.None, 0.09f);
            spriteBatch.End();

            base.Draw(gameTime);
        }
    }


C#-Quelltext

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
    public class Camera
    {
        public Vector2 PlayerPosition;
        private Vector2 Viewport;
        private float Scale;

        public Camera(Vector2 playerposition, float scale, Vector2 viewport)
        {
            PlayerPosition = playerposition;
            Scale = scale;
            Viewport = viewport;
        }

        public void Update(GameTime gameTime, Vector2 playerposition, float scale, Vector2 viewport)
        {
            PlayerPosition = playerposition;
            Scale = scale;
            Viewport = viewport;
        }

        public Matrix GetMatrix()
        {
            return Matrix.CreateTranslation(new Vector3(-PlayerPosition.X, -PlayerPosition.Y, 0))
         * Matrix.CreateScale(Scale, Scale, 1)
         * Matrix.CreateTranslation(Viewport.X / 2, Viewport.Y / 2, 0);
        }
    }

BlueCobold

Community-Fossil

Beiträge: 10 738

Beruf: Teamleiter Mobile Applikationen & Senior Software Engineer

  • Private Nachricht senden

12

02.11.2018, 12:22

Also... BalkenPosOben und BalkenPosUnten hätte ich ja berechnet mit 0 und (ScreenHeight - BalkenHöhe). Keine Ahnung, wie du da auf (gameWorldSize.Y/2...) kommst. Und auch nicht, wozu du eine skalierte Kamera erzeugst. Davon war nie die Rede in meiner Beschreibung.
Teamleiter von Rickety Racquet (ehemals das "Foren-Projekt") und von Marble Theory

Willkommen auf SPPRO, auch dir wird man zu Unity oder zur Unreal-Engine raten, ganz bestimmt.[/Sarkasmus]

13

02.11.2018, 15:54

Ich habe den Code geändert(jetzt ohne Camera) jedoch funktioniert es trotzdem noch nicht :( Die Balken werden immer noch nicht an der richtigen Position gezeichnet und die Hintergrundgrafik wird dadurch jetzt nicht mehr in der Mitte des Bildschirms gezeichnet und ich weiss nicht wieso ?(

C#-Quelltext

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
public class Game1 : Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        Vector2 vp, gameWorldSize = new Vector2(1334, 750); 
        float ScaleX, ScaleY, Scale;
        Texture2D Hintergrund, Balken;
        float BalkenPosOben, BalkenPosUnten, BalkenBreite, BalkenHoehe;
        Matrix Scalematrix;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            graphics.IsFullScreen = true;
            graphics.SupportedOrientations = DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight;
        }

        protected override void Initialize()
        {
            base.Initialize();
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);        
            Hintergrund = Content.Load<Texture2D>("Hintergrundbild");
            Balken = Content.Load<Texture2D>("Balkenschwarz");
            vp = new Vector2(GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);
            ScaleX = vp.X / gameWorldSize.X;
            ScaleY = vp.Y / gameWorldSize.Y;
            Scale = Math.Min(ScaleX, ScaleY);
            Scalematrix = Matrix.CreateScale(Scale, Scale, 1.0f);
            float ScreenHoehe = GraphicsDevice.Viewport.Height;
            float Bildhoehe = Hintergrund.Height;
            float Differenz = ScreenHoehe - Bildhoehe * Scale;
            BalkenBreite = GraphicsDevice.Viewport.Width;
            BalkenHoehe = Differenz / 2;
            BalkenPosOben = (int)0 + BalkenHoehe / 2;
            BalkenPosUnten = (int)ScreenHoehe + BalkenHoehe / 2;
        }

        protected override void Update(GameTime gameTime)
        {

            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend, null, null, null, null, Scalematrix);
            spriteBatch.Draw(Hintergrund, new Rectangle((int)gameWorldSize.X / 2, (int)gameWorldSize.Y / 2, Hintergrund.Width, Hintergrund.Height), null, Color.White, 0, new Vector2(Hintergrund.Width / 2, Hintergrund.Height / 2), SpriteEffects.None, 0.10f);
            spriteBatch.Draw(Balken, new Rectangle((int)gameWorldSize.X / 2, (int)BalkenPosOben, (int)BalkenBreite, (int)BalkenHoehe), null, Color.White, 0, new Vector2(Balken.Width / 2, Balken.Height / 2), SpriteEffects.None, 0.09f);
            spriteBatch.Draw(Balken, new Rectangle((int)gameWorldSize.X / 2, (int)BalkenPosUnten, (int)BalkenBreite, (int)BalkenHoehe), null, Color.White, 0, new Vector2(Balken.Width / 2, Balken.Height / 2), SpriteEffects.None, 0.09f);
            spriteBatch.End();

            base.Draw(gameTime);
        }
    }

Sylence

Community-Fossil

Beiträge: 1 663

Beruf: Softwareentwickler

  • Private Nachricht senden

14

02.11.2018, 17:36

Debugger anschmeißen und dir die berechneten Werte angucken?

15

02.11.2018, 17:47

Hab den Fehler gefunden.

Dieser Beitrag wurde bereits 3 mal editiert, zuletzt von »Big_Santa« (04.11.2018, 00:26)


Werbeanzeige