I am having some issues with regards to making a picturebox display.
I am working on a simple RPG type game (a prototype), and the player can make a character. The character class has a picturebox attached to it, and when they create a character, an image is selected for this picturebox.
Once the game starts, the character spawns on a location. However, the picturebox doesn't show. Whilst on a location, the character can move around. After a random number of steps, combat is initiated, and the combat screen loads. Even though the picturebox doesn't load, the character seems to move around, and combat does get initiated.
When I leave combat and return to the form, the picturebox for the character appears. I have other pictureboxes on the location form, and they load.
The constructor for a location has a call to a method called initialise. Here is some relevant code from this method:
public void Initialise()
{
c = StateManager.C;
StepCount = 0;
SetStepsTillCombat();
EntranceLocation = "";
SetStartPosition();
c.Pic.Location = NewLocation ;//c.Pic is the picturebox
this.Controls.Add(c.Pic);
The set start position method aims to determine where the player should spawn. The code for this is as follows:
public void SetStartPosition()
{
switch (EntranceLocation)
{
case "North":
StartLocation.X = this.Width/2;
StartLocation.Y=10;
NewLocation = StartLocation;
break;
case "South":
StartLocation.X = this.Width/2;
StartLocation.Y=this.Height - c.Pic.Height;
NewLocation = StartLocation;
break;
case "West":
StartLocation.X = 10;
StartLocation.Y=this.Height/2;
NewLocation = StartLocation;
break;
case "East":
StartLocation.X = this.Width-c.Pic.Height;
StartLocation.Y=this.Height/2;
NewLocation = StartLocation;
break;
case "":
StartLocation.X = this.Width / 2;
StartLocation.Y = this.Height / 2;
NewLocation = StartLocation;
break;
}
}
I can't remember why I am even using startlocation, instead of setting newlocation directly, there was a reason, and then I removed the feature. Anyway, depending on where the player enters the location, their spawn point will differ, or if the player enters the first location when starting the game, they start at a set point. I call this method first, before assigning the location of the player picturebox.
When first entering a location, there is no picture. When I leave combat, I have the following code:
public void EndClick(object sender, EventArgs e)
{
this.Hide();
c.NumberOfBattles++;
returnLocation.Spawn(c.Location);//Coordinates for where to spawn the player
returnLocation.SetStepsTillCombat();
returnLocation.StepCount = 0;
returnLocation.Show();//Load the location I entered combat from
}
The spawn method looks like:
public void Spawn(Point p)
{
c.Location = p;
c.Pic.Location = c.Location;
this.Controls.Add(c.Pic);
}
When doing this, c.Pic does display.
I can't see what I am doing wrong here, any input would be appreciated. I think I have included all relevant code.
EDIT:
I found a work around, whereby each time I move the character, I call the spawn method, though this seems like an inefficient way to do things.
Related
So, firstly, my scene is made out of 9 empty objects each one having spikes that have animation to pop out of the floor. I am making a game where you should avoid spikes and other projectiles. I tested this first with sprite renderer and changing colors, it worked perfectly. But when I want to activate animations using trigger (animations start from empty game state and go to their animations, then after it finishes they return to normal). I've looked trough every thing I could think of and I could not solve this. It always starts animations for 4-6 different animations and start them at the same time, but for some reason: AFTER FIRST SHOWING, every single time after first time spikes appear, 2/3 out of 4/6 always show late but start at same time. Weirdly first time it always works greatly. Any thoughts on what may cause this?
void Update()
{
if (spikeTimer > 0)
spikeTimer -= Time.deltaTime;
if (spikeTimer<0)
{
Function();
spikeTimer = spikeCooldown;
}
}
public void Function()
{
int x = Random.Range(4, 6);
List<int> included = new List<int>();
while (included.Count < x)
{
int y = Random.Range(1, 10);
if (!included.Contains(y))
{
included.Add(y);
}
}
foreach (int i in included)
{
print("aktiviran je broj" + i);
Spikes[i - 1].GetComponent<Spike>().ActivateSpike();
}
}
Now this is the ActivateSpike method that all Spikes in the scene contain
public void ActivateSpike()
{
SpriteRenderer sr = gameObject.GetComponent<SpriteRenderer>();
if (sr.color != Color.black)
{
sr.color = Color.black;
}
else
sr.color = Color.red;
/* Animator animator = gameObject.GetComponent<Animator>();
animator.SetTrigger("Activate");*/
}
You can see that I tried changing the sprite to square and changing its color, and it works perfectly...
In the title it is not easy to summarize this behavior in the best way.
I have an Editor and a Monobehaviour executed in [ExecuteInEditMode].
I need to move objects by selecting them with the mouse. I thought I didn't encounter any difficulties as I perform this operation with both a character and sprite. But I was wrong. When I select a 3D object (not the character) I can pick and drag it but if I hold the mouse button down without moving it it tends to slip away as if it were falling but on the Y and Z axes. If I deselect its collider the issue does not happen but obviously the collider is necessary for my operations.
I have removed all the unnecessary lines from the code, hoping to solve the problem, but it remains.
void OnGUI() {
if (moveChar) {
StartCoroutine("WaitChar");
} else if (moveSpot) {
StartCoroutine("WaitSpot");
}
Event e = Event.current;
Vector3 mousePosition = e.mousePosition;
mousePosition.y = Screen.height - mousePosition.y;
Ray ray = cam.ScreenPointToRay(mousePosition);
RaycastHit hitInfo;
if (Physics.Raycast(ray, out hitInfo)) {
mousePositionWorld = hitInfo.point;
if (e.rawType == EventType.MouseDown) {
switch (e.button) {
case 0:
if (isSelectedSphere) {
moveSpot = true;
}
break;
case 1:
if (isSelectedChar) {
moveChar = true;
}
break;
}
} else if (e.rawType == EventType.MouseUp) {
switch (e.button) {
case 0:
if (moveSpot) {
moveSpot = false;
}
break;
case 1:
if (moveChar) {
moveChar = false;
}
break;
}
}
}
}
public IEnumerator WaitChar() {
character.transform.position = mousePositionWorld;
yield return new WaitForSeconds(0.1f);
}
public IEnumerator WaitSpot() {
MoveSpot.transform.position = mousePositionWorld;
MoveSpot.transform.localScale = Vector3.one * ((new Plane(cam.transform.forward,
cam.transform.position).GetDistanceToPoint(
MoveSpot.transform.position)) / radiusPoint);
yield return new WaitForSeconds(0.1f);
}
I have simplified everything with two public variables to choose the object to move.
What I do is very simple, I don't understand why I get this behavior. With the character I have no problem (not even with the sprites, which for the moment I removed from the code). When I click with the right mouse button I run the coroutine that moves the character to the cursor coordinates and continues like this until I release the button. No problem, the character moves along with the cursor without problem.
However, if I execute the same operation (left button) with a 3D object like Sphere, Cylinder, Cube it moves following the mouse but if I don't move the cursor and don't release the button the object slides away with the Y and the Z axes that increase in negative. As I said, if I deactivate the collider the object remains at the cursor position.
The problem is that the spheres cannot be selected through public variables but directly from the script so I need the collider.
I probably don't know Unity well enough to know the reason for this behavior.
Can you clarify this for me?
OnGui is bad (let me explain)
First, OnGui is part of Unity's IMGui system (stands for IMmediate Gui), and has been heavily depreciated in favor of uGui, which is a lot easier to work with for a number of reason. And one of those reasons is....
OnGui is called more than once per frame
Because OnGui is called multiple times per frame, that means that Event.current hasn't changed, meaning moveChar hasn't changed, meaning your coroutine gets started more than once.
And there's no real way around this. Its going to be almost impossible to do what you want using OnGui. It was never meant to handle this sort of logic.
Everything you have should be done inside Update() instead and replacing things like e.rawType == EventType.MouseDown with their Input relatives, eg. Input.GetMouseDown(0).
I am trying to achieve a very basic 2D game right now where an enemy is on the screen and it bounces back and forth between set points (50 and 500) kind of like a space invaders sort of thing. My issue is I can only get it to go right, but then not come back towards the left and repeat.
I was messing around with coding it myself before bothering to look into it and actually figure it out but I thought I had something that would work, but well it doesn't, my issue is I don' get why.
My code is supposed to work like a switch, two if statements within the Update loop, one comes on the other goes off, one moves it right the other moves it left, I thought that was fine but it doesn't do that. It moves it right just fine but then the left part just doesn't work.
So, why does the following code not work?
namespace _2D_game_num1
{
class Enemy
{
int health;
Vector2 enemy_location = new Vector2(50, 50);
Vector2 enemy_speed = new Vector2(1, 1);
Player player = new Player("dummy");
public Enemy()
{
health = 100;
}
public void UpdateLocation()
{
//Vector2 player_pos = player.GetLocation();
//if (player_pos.X < 200)
// Using the players location to figure out where the enemy should move
bool right = true;
bool left = false;
if (right)
{
enemy_location.X += enemy_speed.X;
if (enemy_location.X == 500)
{
right = false;
left = true;
}
}
if (left)
{
enemy_location.X -= enemy_speed.X;
if (enemy_location.X == 50)
{
right = true;
left = false;
}
}
}
public Vector2 GetLocation()
{
return enemy_location;
}
}
}
And then in the main Game class I have it so enemy1.UpdateLocation(); is within the Update section correctly (along with my players movement which works fine).
Try this:
public void UpdateLocation()
{
enemy_location.X += enemy_speed.X;
if (enemy_location.X <= 50 || enemy_location.X >= 500)
{
enemy_speed.X = new Vector2(-enemy_speed.X, enemy_speed.Y);
}
}
What we are doing here is moving the enemy based on it's current speed. Then if the location is on the left or right of the screen, change direction.
Sometimes it pays to keep things simple. Get rid of the left and right flags, as they are just confusing things. When you're dealing with Update methods you're typically changing the state of something. The way you had it before, the left and right state was getting reset every time UpdateLocation is called.
Btw, you might want to consider passing in GameTime to your Update methods. Typically, when you're moving things around in real-time you'll want to multiply movement by some kind of deltaTime to keep things smooth on all devices. You're probably getting away with it because by default it'll be a fixed frame rate, but that may not always be the case, so it's a good habit to get into.
i built a space invaders game, and the problem is , that the hit sensitivity is way too sensitive. Here is how i trace whether the player was hit.
bool playersWasShot=false;
foreach (var shotsInvader in Invadershots)
{ // below is the Area representing the image size. and the location of the invaders shot
if(playership.Area.Contains(shotsInvader.Location))
{
//player was shot game over.
playersWasShot=true;
break;
}
}
The area property from the PlayerShip class:
// move simply updates the position of the ship on the x-axis as i move the ship.
// as you can guess. the second variable represents the size of the picture
Area =new Rectangle(new Point(move,900), Properties.Resources.player.Size);
The shot location property in the Shots class is being updated as the shot travels to the
player..
// the shot, travels to the player on the y-axis only
Location=new Point(Location.X, invaderShotLocation);
i checked in the debugger when the game is over..and this is what i got:
This line:
if(playership.Area.Contains(shotsInvader.Location))
{
//i put a debugging point inside this if statment!!!
}
represents info on the ship:
player Area {X=90,Y=900,Width=54,Height=33}
Location {X=60,Y=900}
represents info on the invaders shot:
Location {X=140,Y=900}
The shot didnt even go inside the players ship..how come
public static Bitmap SHIP = Properties.Resources.player;
public Point Location;
public int move=10;
public Rectangle Area { set { } get { return new Rectangle(new Point(move, 900), Properties.Resources.player.Size); } }
public void Draw(Graphics g)
{
Those are the 2 methods in the ship class that govern all the movement and the drawing
if (game.Alive) //checks if the player is alive.
{
// draws the SHIP picture. 900 is a fixed field..thats where the ship is
// it is on the y axis that never changes.. x-axis do change..depending
// on where i move the ship to.
g.DrawImage(SHIP, move, 900);
Location = new Point(move, 900);
}
else
{
g.DrawImage(SHIP, move, 900, SHIP.Width, SHIP.Height);
Location = new Point(move, 900);
}
}
public void Move(Direction d)
{
switch (d)
{
// an enum is passed when i press the arrows indicating to move left or right
case Direction.LEFT: move -= 10; break;
case Direction.RIGHT: move += 10; break;
}
Area = new Rectangle(new Point(move, 900), SHIP.Size);
}
NOTE= SAME LOGIC WAS APPLIED WHEN AN INVADER IS BEING HIT BY THE PLAYER, AND THE INVADER DOES DISAPPEAR ONLY WHEN THE SHOT HITS HIM
Bug found in the Shot class. the players and invaders shots share:
..i found the bug. it was in the shot class. i modified it now. issue has been solved
It seems like the shot did go into the ship. The shot is at (140, 900) and the ship's Area corners are at (90, 900) and (144, 900). (140, 900) is inside of that.
What is the "Location {X=60,Y=900}" part listed after the ship's Area listing? Are you perhaps painting the ship in a different spot than the hit test Area?
Sounds like you have a bug in PlayerShip.Area.Contains. Double check the logic.
I'm trying to learn C# (so go easy on me!) and as part of a coursework I have been asked to create a game called strikeout.
The game rules are very simple. It is simply a case of knocking one counter into another, with the aim of leaving one counter left on the screen.
I am having trouble simply moving one counter into another. I have created a number of picture boxes using a loop (to make up my board), and created an event handler using the loop. How can I use the single even handler to move all of my counters?
{
(...)
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
int x = j * (75);
int y = i * (75);
pictureBoxCounter[j, i] = new PictureBox();
pictureBoxCounter[j, i].Location = new System.Drawing.Point(x, y);
pictureBoxCounter[j, i].Size = new System.Drawing.Size(75, 75);
pictureBoxCounter[j, i].BackColor = System.Drawing.Color.Transparent;
panelGame.Controls.Add(pictureBoxCounter[j, i]);
this.pictureBoxCounter[j, i].Click += new System.EventHandler(this.pictureBoxCounter_Click);
}
}
} // end of some function
private void pictureBoxCounter_Click(object sender, EventArgs e)
{
//I need some code here but nothing seems to work :(
}
I've spent way to long on this problem. Even asked my tutor to help. Instead of helping he managed to break most of my code. So after fixing the problems he caused I am now back with a compiling program!
(When running the program you will need to enter player info to enable the start game button.)
If I can help with any other information don't hesitate to ask!
This programming problem appears intended to teach you about separation of concerns. If it isn't, it should be. (It is now!)
You have two problems, not one. (Actually, you have more than two, but you have two big ones.) One problem is: How do I create a set of objects representing the counters in this game, and what rules do those objects follow? The other problem is: How do I represent the counters on the screen?
If your solution to the first problem is to create a bunch of PictureBoxes, you're going down a rabbit hole that it's going to be tough to get back out of. You should solve the first problem first, and then the second problem.
Here's a rough sketch (very rough, because I don't know the rules of this game) of an object model that addresses the first problem:
public class Board
{
public const int Height = 8;
public const int Width = 8;
private Counters[Height][] Counters { get; set; }
public Counter GetCounter(int row, int col)
{
return Counters[row][col];
}
public void Initialize() { }
public void ExecuteMove(Counter c) { }
}
public class Counter
{
public int Row { get; set; }
public int Column { get; set; }
}
So, a Counter object knows where it is (its Row and Column). A Board object knows about all of the Counter objects, it knows how to find a Counter given its row and column, and it knows how to execute a move when a counter gets clicked on. (I don't know anything about the rules of the game, but they're going to live in the ExecuteMove method.
You can now trivially write a method that prints the board to the console:
public void PrintBoard(Board b)
{
for (int col = 0; col < board.Width; col ++)
{
for (int row = 0, row < board.Height; row++)
{
Counter c = board.GetCounter[row][col];
Console.Write(c == null ? " " : "*");
}
Console.WriteLine();
}
}
and a method to input a move:
public Counter InputMove(Board b)
{
string s;
Console.Write("Row: ");
s = Console.ReadLine();
int row = Convert.ToInt32(s);
if (s == "") return null;
Console.Write("Column: ");
s = Console.ReadLine();
if (s == "") return null;
int column = Convert.ToInt32(s);
return b.GetCounter(row, column);
}
...and now you have everything you need in order to code and test the ExecuteMove method. Get that working. I'll wait.
You done? Good. There are a couple of problems that you probably ran into that I haven't addressed. For instance, you probably discovered that when you move a Counter, you have to update both the board's array and the Counter itself. And you also probably discovered that you have to come up with some way of keeping track of what happens when a Counter is destroyed. Aren't you glad you weren't screwing around with mouse clicks and UI elements too?
Now for part two. Let's make a UI that knows how to talk to the Board. In your form, loop through the counters in the board, create a PictureBox for each (with its position based on the Row and Column properties), and add it to a Dictionary<PictureBox, Counter> called, say, Counters. You'll also want a Dictionary<Counter, PictureBox> called, say, PictureBoxes. These maps give you the ability to find a Counter given its PictureBox, and vice versa.
Attach this event handler to each PictureBox:
private void PictureBox_Click(object sender, EventArgs e)
{
PictureBox p = (PictureBox) sender;
Counter c = Counters[p];
Board.ExecuteMove(c);
}
(This, by the way, is the answer to your original question.)
Well, that's all well and good: you can click on a PictureBox, find its Counter, and execute the move. But how do you update the visual state of the game in the UI? That depends, a lot, on what the actual rules of the game are.
For instance, if clicking on a counter makes it disappear, you might add a Visible property to the Counter class and have the Board class's ExecuteMove update it. If, when a Counter becomes invisible, the other Counters in its row move left or right, the ExecuteMove method will have to update their Row and Column, and do something with the now-invisible Counter that was at that position.
But you already worked all of this out back when you tested all of the game logic, right? All you need to implement in your UI is an equivalent method to the one you built for printing it to the console. Only this one iterates through all of the Counters in the board, and updates their PictureBox based on their state.
There are a lot of subtleties that I'm glossing over here, mostly because a) I don't fully understand your problem and b) I'm not actually trying to do your homework for you.
You'll need to find the clicked picture box back in the Click event handler. You can use the sender argument for that. Trivially:
private void pictureBoxCounter_Click(object sender, EventArgs e)
{
PictureBox box = sender as PictureBox;
box.BackColor = System.Drawing.Color.Coral;
}
But you'll need to implement your game logic as well, which will require you know the box's location on the game board. You could use the PictureBox.Tag property for that. It could store a Point for example:
...
pictureBoxCounter[j, i].BackColor = System.Drawing.Color.Transparent;
pictureBoxCounter[j, i].Tag = new Point(j, i);
panelGame.Controls.Add(pictureBoxCounter[j, i]);
...
private void pictureBoxCounter_Click(object sender, EventArgs e)
{
PictureBox box = sender as PictureBox;
box.BackColor = System.Drawing.Color.Coral;
Point pos = (Point)box.Tag;
int row = pos.X;
int col = pos.Y;
//etc...
}
But you probably want to use a helper class that stores more info than just the row and column number.