簡體   English   中英

XNA-檢測與數組對象的沖突

[英]XNA - detecting collision with array objects

因此,我正在XNA中制作“太空侵略者”克隆。 我需要做碰撞檢測邏輯。 我的入侵者被關押在一個陣營中。 每個入侵者都有自己的矩形邊界框。 因此,如果我的船碰到一個侵略者,它應該松懈或采取類似措施。 這是我的入侵者職業:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Graphics.PackedVector;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Design;

namespace SpaceInvaders
{
    public class botInvaders
    {
        public botInvaders()
        {

        }

        public static Texture2D botInvaderTex;
        public static Rectangle botInvaderHitBox;
        public static Vector2 botInvaderOrigin;
        public int botInvaderCurrentFrame = 1, botInvaderFrameWidth = 52, botInvaderFrameHeight = 90, invaderRows = 3, invaderCollumns = 10; // invaderRows = 5 // For 50 invaders
        float botInvadersTimer = 0f, botInvadersInterval = 100;
        public static Rectangle[,] botInvadersRect;
        String botInvadersDirection = "RIGHT";
        public static Color invadersColor = Color.White;
        SoundEffect invaderTeletransportation;
        SoundEffectInstance invaderTeletransportationInstance;

        public void LoadContent(ContentManager Content)
        {
            botInvaderTex = Content.Load<Texture2D>(".\\gameGraphics\\gameSprites\\botInvaders\\normalInvaders\\invaderShip1");
            invaderTeletransportation = Content.Load<SoundEffect>(".\\gameSounds\\teletransportation");
            invaderTeletransportationInstance = invaderTeletransportation.CreateInstance();
            invaderTeletransportationInstance.IsLooped = false;
            botInvadersRect = new Rectangle[invaderRows, invaderCollumns];
            for (int r = 0; r < invaderRows; r++)
            {
                for (int c = 0; c < invaderCollumns; c++)
                {
                    botInvadersRect[r, c].Width = botInvaderFrameWidth;
                    botInvadersRect[r, c].Height = botInvaderTex.Height;
                    botInvadersRect[r, c].X = 70 * c;
                    botInvadersRect[r, c].Y = (70 * r) + 22;
                }
            }
        }

        public void Update(GameTime gameTime)
        {
            botInvaderHitBox = new Rectangle(botInvaderCurrentFrame * botInvaderFrameWidth, 0, botInvaderFrameWidth, botInvaderFrameHeight);
            botInvaderOrigin = new Vector2(botInvaderHitBox.X / 2, botInvaderHitBox.Y / 2);

            botInvadersTimer += (float)gameTime.ElapsedGameTime.Milliseconds;

            if (botInvadersTimer > botInvadersInterval)
            {
                botInvaderCurrentFrame++;
                botInvadersTimer = 0f;
            }

            if (botInvaderCurrentFrame == 2)
            {
                botInvaderCurrentFrame = 0;
            }

            if (Game1.gameStart == 2)
            {
                invaderTeletransportationInstance.Play();
            }

            botInvaderHitBox = new Rectangle(botInvaderCurrentFrame * botInvaderFrameWidth, 0, botInvaderFrameWidth, botInvaderFrameHeight);
            botInvaderOrigin = new Vector2(botInvaderHitBox.Width / 2, botInvaderHitBox.Height / 2);
        }

        public void Draw(Texture2D invadersTex, Rectangle[,] invadersDestinationRect, Nullable<Rectangle> invadersSourceRect, Color invadersColor, float invadersRotation, Vector2 invadersOrigin, SpriteEffects invadersEffects, float invadersScale, SpriteBatch spriteBatch)
        {
            for (int r = 0; r < invaderRows; r++)
            {
                for (int c = 0; c < invaderCollumns; c++)
                {
                    spriteBatch.Draw(botInvaderTex, botInvadersRect[r, c], botInvaderHitBox, Color.White);
                }
            }
        }
    }
}

ship類基本上是相同的,但沒有數組(只有一個):因此,它也有一個playerShipHitBox

Update()方法中,您可以執行簡單的矩形相交檢查:

for (int r = 0; r < invaderRows; r++)
    for (int c = 0; c < invaderColumns; c++)
        if (botInvadersRect[r, c].Intersects(playerShipHitBox))
            // BAM! Collision!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM