開発したアプリなど一覧

自機と敵の向きを合わせる。

アフィリエイトリンクを含む場合があります

http://www13.plala.or.jp/kmaeda/xna/shiprot.htm

演習にあったのでちょっとやってみた。自機とか敵とかいってるけど様はキャラクター二つ向かい合わせるということ。

サンプルプログラム内にあった Set_rot() メソッドちょっと改良して自機と敵キャラを同時に回転させればOK。回転させるので画像の中心点は長さ/2などとして正確に求めておく。

こんな感じになる。背景は気にしちゃだめです。ACの画像編集ちょっとミスって一部透過してないけどこれも気にしちゃだめです。
xna

xna

ちなみに以下のスクリプトだと反対側に移動するとこうなっちゃう・・・
xna

以下ソース

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

namespace WindowsGame4 { public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; SpriteBatch spriteBatch;

Texture2D texture;

Texture2D texture_ac; Vector2 ac_pos; Vector2 ac_org; float ac_rot = 0;

Texture2D texture_miku; Vector2 miku_pos; Vector2 miku_org; float miku_rot = 0;

public Game1() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; graphics.PreferredBackBufferWidth = 1024; graphics.PreferredBackBufferHeight = 768; this.IsMouseVisible = true; }

protected override void Initialize() { base.Window.Title = "XNA Game Studio"; base.Initialize(); }

protected override void LoadContent() { spriteBatch = new SpriteBatch(GraphicsDevice);

texture = Content.Load<Texture2D>("makinami"); texture_ac = Content.Load<Texture2D>("ac"); texture_miku = Content.Load<Texture2D>("miku"); ac_pos = new Vector2(200, 200); ac_org = new Vector2(texture_ac.Height / 2, texture_ac.Width / 2); miku_pos = new Vector2(400, 200); miku_org = new Vector2(texture_miku.Height / 2, texture_miku.Width / 2); }

protected override void UnloadContent() { }

protected override void Update(GameTime gameTime) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) this.Exit();

KeyboardState keyState = Keyboard.GetState(); if (keyState.IsKeyDown(Keys.Left)) miku_pos.X -= 4; if (keyState.IsKeyDown(Keys.Right)) miku_pos.X += 4; if (keyState.IsKeyDown(Keys.Up)) miku_pos.Y -= 4; if (keyState.IsKeyDown(Keys.Down)) miku_pos.Y += 4;

ac_rot = Set_rot(ac_pos, miku_pos, 0); miku_rot = Set_rot(miku_pos, ac_pos, 180);

base.Update(gameTime); }

public float Set_rot(Vector2 Pos, Vector2 Pos2, float offset) { float dx, dy, rot; dx = Pos2.X - Pos.X; dy = Pos2.Y - Pos.Y; rot = (float)Math.Atan2((double)dy, (double)dx); return rot + MathHelper.ToRadians(offset); }

protected override void Draw(GameTime gameTime) { graphics.GraphicsDevice.Clear(Color.CornflowerBlue); spriteBatch.Begin(SpriteBlendMode.AlphaBlend); spriteBatch.Draw(texture, new Vector2(0.0f, 0.0f), Color.White); spriteBatch.Draw(texture_ac, ac_pos, null, Color.White, ac_rot, ac_org, 1.0f, SpriteEffects.None, 1.0f); spriteBatch.Draw(texture_miku, miku_pos, null, Color.White, miku_rot, miku_org, 1.0f, SpriteEffects.None, 1.0f); spriteBatch.End(); base.Draw(gameTime); } } }

Sponsored Link

コメント

タイトルとURLをコピーしました