If Rectangle doesn't Contains Mouse Position - windows

I have a Rectangle which I can touch with this command below.
if ((mouse.LeftButton == ButtonState.Pressed )&&
TextureRectangle.Contains((int)MousePos.X,(int)MousePos.Y))
{
// Action;
}
But is there a Command like "Not Contains", so I wanna do something else if the user touch out of the "TextureRectangle" area?
When I click to the Rectangle that both actions starts. I really dont know where the problem is.
if (mouse.LeftButton == ButtonState.Pressed){
if(TextureRectangle.Contains((int)MousePos.X, (int)MousePos.Y)) {
music1.Play();
}
else{
music2.Play();
}
}
my problem is that music1 and music2 plays at same time if i click on the Rectangle, i want that when i click on the Rectangle that music1 plays only (here is the problem , both starts to play)and when i click out of the Rectangle should start only music2 to play ( this case is ok)

I would strongly recommend you to get a programming book / ebook and start reading it. This is basic computer logic stuff.
if (mouse.LeftButton == ButtonState.Pressed)
{
if (TextureRectangle.Contains((int)MousePos.X, (int)MousePos.Y))
{
// inside
}
else
{
// outside
}
}
OR
if (mouse.LeftButton == ButtonState.Pressed)
{
if (!TextureRectangle.Contains((int)MousePos.X, (int)MousePos.Y))
{
// outside
}
else
{
// inside
}
}

Related

Creating click to start screen for game

I am using Processing to create a basketball game. I have managed to create the basketball game but I want to have a click to start home screen. I have made the graphic for the home screen but I am not sure how to integrate it into the game code. Any ideas on how to go about this. Thanks!
I found something on the internet related to this which was...
if (started) {
//all the code for the game
} else {
// all the code for the start screen
if (keyDown("enter")) {
started = true;
}
}
Im not sure if this is leading me in the right direction or how I could necessarily use this.
Use keyPressed() outside the draw().
void keyPressed() {
println(int(key));
if (key==10) {
println("ENTER");
}
}
key - contains the value of the most recent key on the keyboard that was used (either pressed or released).
What is described in: https://processing.org/reference/keyPressed_.html
Here is a little demo program that may help you. The global variable started controls whether the game has begun or not. The function keyPressed sets it to true if you press the ENTER key.
In draw, put your game code in the first if-part and your waiting screen in the second.
boolean started = false;
void draw() {
background(0);
textAlign(CENTER);
if (started) {
// your game code here
text("gameplay", 50, 50);
} else {
// your start/launch screen here
text("waiting...", 50, 50);
}
}
void keyPressed() {
if (keyCode == ENTER) {
started = true;
}
}

Implement a sharp right turn using atmega8 for line follower

I am new to AVR programming, and I am trying to implement a sharp right turn using atmega8. I was able to implement the straight line path but cannot implement a sharp right turn. Here is my code:
`#include <avr/io.h>
#include<util/delay.h>
int main(void)
{
DDRC=0b00000000;
DDRB=0b11111111;
int count=1,right=1;
while(1)
{
if((PINC&=0b00011111)==0b00000000)
{
PORTB=0b00000110;
}
else if((PINC&=0b00011111)==0b00001110)
{
PORTB=0&00100111;
}
else if((PINC&=0b00011111)==0b00001100)
{
PORTB=0b00000111;
}
else if((PINC&=0b00011111)==0b00000110)
{
PORTB=0b00100110;
}
else if((PINC&=0b00011111)==0b00001111)
{
if(count)
{
PORTB=0b0010011;
_delay_ms(200);
count--;
}
else if(((PINC&=0b00011111)==0b00000110)&&~(count))
{
PORTB=0B00000111;
}
}
else if((PINC&=0b00011111)==0b00011110)
{
if(right)
{
PORTB=0b0010011;
_delay_ms(200);
right--;
}
else if(((PINC&=0b00011111)==0b00000110)&&~(right))
{
PORTB=0B00100110;
}
}
}
}
This doesn't seem to work at all for right and left turns.
Any idea where I am going wrong?
Without understanding your Program (see my Comment above) iam guessing it is because you permanently write to your PIN-regsiters in the if-clauses.
PINC&=0b00011111 means:
read PINC-value
binary AND it with 0b00011111
write the result back to PINC
Depending on which AVR your code is running you toggle the output by writing a 1 to a PINX-register bit. If DDR is configured to input you toggle the pullups. This is true for the newer AVR-cores. For the old one its undefined behavior to write to the PIN-registers as they are defined as read-only.

I have two 3dtext, one plays the animation, the other reverses it. After one go, the animation wont play anymore, why?

I have a 3dtext named Play, which when clicked will play the animation; the other one is named Back, which reverses the animation. Problem is after I Played and Backed it, the animation wont play anymore when i clicked Play.
The animation named redsubmenu is in legacy and clamp forever wrap mode.
public class PlayButtonScript : MonoBehaviour {
//public static PlayButtonScript pbs;
public GameObject redsubmenu;
void Update(){
#if UNITY_EDITOR
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(Input.GetMouseButtonDown(0)&&Physics.Raycast(ray,out hit)){
if(hit.collider.name == "Play"){
redsubmenu.animation.Play();
}
}
#endif
}
}
public class BackButtonScript : MonoBehaviour {
// Update is called once per frame
void Update () {
#if UNITY_EDITOR
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(Input.GetMouseButtonDown(0)&&Physics.Raycast(ray, out hit)){
if(hit.collider.name == "Back"){
transform.parent.animation["redsubmenu"].speed = -1;
transform.parent.animation.Play("redsubmenu");
}
}
#endif
}
}
It appears that you never reset the speed of the animation back to 1. When you click play the first time the speed is initially 1, so it works fine. However, when you back you set the speed to -1 and it is never set to any other value.
Try using:
if (hit.collider.name == "Play") {
transform.parent.animation["redsubmenu"].speed = 1;
redsubmenu.animation.Play();
}
in your play button script.
You might also be able to make use of Animation.Rewind.
http://docs.unity3d.com/ScriptReference/Animation.Rewind.html
Just to be more specific, i edited my playbuttonscript as shown below:
if(Input.GetMouseButtonDown(0)&&Physics.Raycast(ray,out hit)){
if(hit.collider.name == "Play"){
if(redsubmenu.animation["redsubmenu"].speed == -1){
redsubmenu.animation["redsubmenu"].speed = 1;
} else {
redsubmenu.animation.Play();
}
}
}
in my back button, i delete the transform.parent.animation.Play, no need for that.

Optimized keyboard controls for XNA game

The code I am using for controlling the four-directional movement of the player's sprite in my 2D game is exhibiting some unwanted affection. I realize that this affection is because the if conditions that are first met will trump the later else ifs... So the directional affection my code shows now is: left > right > up > down.
What kind of affection I want is: the first direction pressed > the second direction pressed > the third direction pressed > the fourth direction pressed.
I also want it to remember what order the keypresses are in untill they're released.
Example:
I hold left, the sprite moves left.
I push up while still holding left, and the sprite immediately moves up.
I release up while still holding left, and the sprite resumes its movement left.
This memory should encompass all four directional keys so that the controls won't feel buggy if the user has "fat fingers".
This is the code I use for movement so far:
if (CurrentKeyboardState.IsKeyDown(Keys.Left) == true)
{
Speed.X = moveSpeed;
Direction.X = moveLeft;
}
else if (CurrentKeyboardState.IsKeyDown(Keys.Right) == true)
{
Speed.X = moveSpeed;
Direction.X = moveRight;
}
else if (CurrentKeyboardState.IsKeyDown(Keys.Up) == true)
{
Speed.Y = moveSpeed;
Direction.Y = moveUp;
}
else if (CurrentKeyboardState.IsKeyDown(Keys.Down) == true)
{
Speed.Y = moveSpeed;
Direction.Y = moveDown;
}
I am thinking I could use a List and just put the direction pressed (left, right, up, down) as strings into the list if it isn't already in the list, and then always check what the latest item in the list is to decide what directio to move. And of course remove the strings when the corresponding keys are released. Would this be a good way of solving it?
Here is my attempt on this:
if (currentKeyboardState.IsKeyDown(Keys.Left))
{
if (!keyDownList.Contains("left"))
{
keyDownList.Add("left");
System.Diagnostics.Debug.WriteLine("left inserted");
}
}
else if (oldKeyboardState.IsKeyDown(Keys.Left))
{
keyDownList.Remove("left");
System.Diagnostics.Debug.WriteLine("left removed");
}
if (currentKeyboardState.IsKeyDown(Keys.Right))
{
if (!keyDownList.Contains("right"))
{
keyDownList.Add("right");
System.Diagnostics.Debug.WriteLine("right added");
}
}
else if (oldKeyboardState.IsKeyDown(Keys.Right))
{
keyDownList.Remove("right");
System.Diagnostics.Debug.WriteLine("right removed");
}
if (currentKeyboardState.IsKeyDown(Keys.Up))
{
if (!keyDownList.Contains("up"))
{
keyDownList.Add("up");
System.Diagnostics.Debug.WriteLine("up added");
}
}
else if (oldKeyboardState.IsKeyDown(Keys.Up))
{
keyDownList.Remove("up");
System.Diagnostics.Debug.WriteLine("up removed");
}
if (currentKeyboardState.IsKeyDown(Keys.Down))
{
if (!keyDownList.Contains("down"))
{
keyDownList.Add("down");
System.Diagnostics.Debug.WriteLine("down added");
}
}
else if (oldKeyboardState.IsKeyDown(Keys.Down))
{
keyDownList.Remove("down");
System.Diagnostics.Debug.WriteLine("down removed");
}
try
{
if (keyDownList[keyDownList.Count-1].Contains("left"))
{
//move left
speed.X = moveSpeed;
direction.X = moveLeft;
}
else if (keyDownList[keyDownList.Count-1].Contains("right"))
{
//move right
speed.X = moveSpeed;
direction.X = moveRight;
}
else if (keyDownList[keyDownList.Count-1].Contains("up"))
{
//move up
speed.Y = moveSpeed;
direction.Y = moveUp;
}
else if (keyDownList[keyDownList.Count-1].Contains("down"))
{
//move down
speed.Y = moveSpeed;
direction.Y = moveDown;
}
}
catch (Exception e)
{
}
I had some problems with it initially, but it seems to work fine now with the exception of it generating exceptions (A first chance exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll) while my sprite is standing still. Any tips on how to stop that?
I'm not just looking for a solution that works, but for something durable and efficient that feels rock solid and professional, so discussion on the topic is more than welcome.
Durable, efficient, rock solid, etc. it may not be, but what you're saying with your if/else if block there is that you're only interested in one keystate per frame, when I don't think that's the case.
What happens if you try:
if (CurrentKeyboardState.IsKeyDown(Keys.Down) & !CurrentKeyboardState.IsKeyDown(Keys.Up))
{
Speed.Y = moveSpeed;
Direction.Y = moveDown;
}
if (CurrentKeyboardState.IsKeyDown(Keys.Up) & !CurrentKeyboardState.IsKeyDown(Keys.Down))
{
Speed.Y = moveSpeed;
Direction.Y = moveUp;
}
And repeat similar for left and right. By testing mutual exclusivity between opposing directions, you keep yourself from adding to and subtracting from direction in the same frame. Also, by using separate conditions instead of an if/elseif chain, you allow the possibility to process Left + Up in the same frame.

Java ME. How to animate Sprite

I am having trouble with animating my sprite in Java ME.
if ((k & FIRE_PRESSED) != 0) {
spriteActive = true;
boxer.nextFrame();
if (boxer.getFrame() == boxer.getFrameSequenceLength() - 6) {
spriteActive = false;
}
}
}
// TO re-start a game...
public void update() {
if(boxer.getRawFrameCount() == 5 && spriteActive == false){
boxer.setFrame(0);
}
}
When the enter key is pressed, spriteActive is set true but only changes the frame by one. I intend to have it animating the entire sequence but it is not doing that. Just animates 1 frame at a time with every press.
Does anyone have any ideas/advice of how I should approach this?
Thanks for the time and help!
Do you have any code that checks whether spriteActive is true and then sets the next frame?
I am no expert on Java but I would suspect you'd need to implement something like this:
if (spriteActive == true)
{
boxer.nextFrame();
}

Resources