how to detect touch on a sprite in windows phone 7? - windows

Is there any touches began function in windows phone XNA?
Here I am using a Texture2D and I want to detect the tap on it.
How to do that in xna?

You need to use the TouchCollection class and set a TouchLocation for your Texture2D
Get it's state via:
TouchCollection touch = TouchPanel.GetState()
Then iterate over the TouchLocations in your collection, i guess this will be your texture2D's position:
foreach(TouchLocation tl in touch)
Then inside this loop you can check if the location was touched via the State property of tl e.g.
if(tl.State == TouchLocationState.Pressed)
{
//Execute your code here
}

We wrote our own button class and made certain sprites inherit from this button class. Pretty good way to go about it since the entire sprite then acts as a button.

Related

How I can open a door with animations?

I have this script:
Ray ray = new Ray (cam.transform.position, cam.transform.forward);
RaycastHit hit;
Debug.DrawRay (transform.position, ray.direction * 50f);
if (Input.GetKeyDown (KeyCode.E)) {
if (Physics.Raycast (ray, out hit, 50.0f)) {
if (hit.collider.gameObject.tag == "Door") {
Debug.Log ("YEAH");
}
}
}
How I can start animation open door?
There are a few things that you need to know before you can animate that door.
There are multiple ways to animate an object in Unity. For simple things like that you could decide to just rotate the object (but you need to understand that you will need a Coroutine or a Mathf.MoveTowards or Mathf.Lerp method to avoid instant moving when you call everything in the Update).
But you can also use an animation made by someone else in applications like Blender.
Or finally create an animation with the Unity in game editor and create an Animator to animate your door.
I think that you should use this way.
First follow this official manual:
https://docs.unity3d.com/Manual/animeditor-CreatingANewAnimationClip.html
to create the clip to open your door.
It's really intuitive and you don't even need to code.
After that you should create an Animator Controller:
https://docs.unity3d.com/Manual/class-AnimatorController.html
You will than create a new state with your animation and you will create a connection between the initial state to your "Open door" animation.
After that you just create a simple bool (in your Animator Controller).
And you will add
this.GetComponent().SetBool("nameofyourboolintheanimatorcontroller", true);
to your script (of course this is valid only if your Animator Controller is in the object assigned to your script... if not you should create a new Animator variable and assign it (for example you could make it public and assign it in the editor).

Need help setting up an interface where rotation is fixed for most elements, but alert and sharing boxes auto-rotate with the device

I'm working with Xcode 7 and Swift 2. I am working on an interface with a camera preview layer and controls that display in a manner similar to the native iOS camera app. The controls all stay in place as you turn the device, but the icons "pivot" in place to orient properly for the device orientation. (I hope I explained that in a way that makes sense. If not, open the native camera app on your iPhone and turn the device around a few times to see what I'm talking about.)
I have the basic interface working already by fixing the overall interface orientation using:
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.LandscapeRight
}
Then I use transform to rotate each button for the device orientation.
The problem is: I need to be able to present alert messages (UIAlertController) and a sharing interface (UIActivityViewController) in this same interface. How do I get those items to rotate to the correct orientation while still keeping the rest of the interface static?
As I see it, there are two possible approaches here -- I just don't know how to make either one work:
Set the interface to auto rotate and support all orientations, but disable auto-rotation for the views that I need to keep locked in place.
Set the interface to only allow .landscapeLeft (which is currently how it's set up) and find a way to rotate the alert messages and sharing dialog box.
Got it working. I needed to access presentedViewController.view to rotate the alert and share views.
//used to periodically trigger a check of orientation
var updateTimer: NSTimer?
//Checks if the device has rotated and, if so, rotates the controls. Also prompts the user that portrait orientation is bad.
func checkOrientation(timer: NSTimer) {
//Array of the views that need to be rotated as the device rotates
var viewsToRotate = [oneView, anotherView]
//This adds the alert or sharing view to the list, if there is one
if let presentedVC = presentedViewController?.view {
viewsToRotate.append(presentedVC)
}
//Rotate all of the views identified above
for viewToRotate in viewsToRotate {
switch UIDevice.currentDevice().orientation {
case UIDeviceOrientation.Portrait:
viewToRotate.transform = CGAffineTransformMakeRotation(CGFloat(-M_PI_2))
case UIDeviceOrientation.PortraitUpsideDown:
viewToRotate.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2))
case UIDeviceOrientation.LandscapeRight:
viewToRotate.transform = CGAffineTransformMakeRotation(CGFloat(2 * M_PI_2))
default:
viewToRotate.transform = CGAffineTransformMakeRotation(CGFloat(0))
}
}
}

Unity2D: MissingComponentException: There is no 'Animation' attached to the "Arm" game object

I am trying to make an animation occur every time the space bar is pressed using this code:
using UnityEngine;
using System.Collections;
public class ArmSwipe : MonoBehaviour {
Animation me;
void Start (){
me=GetComponent<Animation>();
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.Space)){
me.Play();
}
}
}
Every time I run it i get this error
MissingComponentException: There is no 'Animation' attached to the "Arm" game object, but a script is trying to access it.
You probably need to add a Animation to the game object "Arm". Or your script needs to check if the component is attached before using it.
UnityEngine.Animation.Play (PlayMode mode)
UnityEngine.Animation.Play ()
ArmSwipe.Update () (at Assets/Scripts/ArmSwipe.cs:18)
EDIT:
Your GameObject Arm needs to have an Animation attached to it. It appears that you don't have such an Animation attached to the Arm. Can you attach a screenshot of your scene graph, and your Arm object's inspector properties?
Also, you probably should use Input.GetKeyUp instead of KeyDown like you currently have. KeyDown will simply cause the animation to re-play on each update, and it will look like it's frozen or stuttering.

XNA Displaying splash screen while content is loading

I'm trying to include splash screen into my XNA project while content is loading. I tried few tutorials but none has helped me. I was trying load content in update method, forcing draw method etc. Since I'm doing my project on Windows PC only I'm looking for a solution to display splash screen (propably animated) in the center of desktop. When content is loaded splash screen should disappear and the game screen should pop up.
I'm looking for some advices and tips how do it since I can't find any help on the Internet.
Personally I was thinking about using Forms but I don't know it well to use it properly.
Thanks in advance.
Since you are loading content, you are probably making a method call that spans over many frames. If you make that call on the GUI thread, you will freeze the game, which isn't what you want.
Instead, call the method inside a Task, and set your game's state as 'loading', and when the task is done, set a state that indicates the task is done.
Then, in your drawing method, you can do something like this:
if (state == State.Loading)
// draw loading screen
else if (State == .....
Extending on what Kendall Fray said, You could use game states like so:
public static GameState currentGameState = GameState.Loading;
public enum GameState
{
Loading, Whatever
}
and on Update or draw:
switch (currentGameState)
{
case (GameState.Loading):
//draw load screen
break;
case (GameState.Whatever):
break;
}

How to move an image

I am creating an app where bombs come into the screen and blow up this thing that you have to move. How do I make these bombs fly into the screen and move in random directions automatically ? Kind of like fruit ninja where the fruits fly out at you. How do I code that ? What do I need to do ? Please also provide some code. Thank you !
Create a class, called Bomb or something and it needs to have an instance variable and property of CGRect on iOS or NSRect on Mac OS.
Then create a method that can draw the bomb on the screen, like - drawOnScreen or just - draw which knows how to draw the object. Which would contain code like [[UIImage imageNamed:#"bomb1"] drawInRect:self.frame]
Then in your game loop you simple change the coordinates of the frame of such a bomb object to make it move, of course you will need to do that before calling the draw method as it would draw the previous frame then..
There also is another way of adding a subview to the gameview every time you need a new bomb. However a view has more overhead than using the object I described above and would most likely cause your game to run slow if you have like 50/60+ ( wild guess ) bombs.
For controls on the screen like a pause button I would definitely use a view and add it as a subview.

Resources