I have a particle system explosion that I want to play when the player dies. The idea that is that everything else pauses but the explosion continues, like the Pacman animation when you die (everything freezes and the pacman death animation plays).
Trouble is, it won't work. I know Time.timeScale = 0 pauses everything, and I've tried using this script to combat that, but it doesn't seem to be working:
void Update()
{
if (Time.timeScale < 0.01f)
{
particleSystem.Simulate(Time.deltaTime, true, false);
}
}
I have also tried this, but it doesn't work either:
private ParticleSystem pSystem;
public void Awake()
{
pSystem = gameObject.GetComponent<ParticleSystem>();
}
public void Play()
{
pSystem.Simulate(Time.unscaledDeltaTime,true,true);
}
public void Update()
{
pSystem.Simulate(Time.unscaledDeltaTime,true,false);
}
I have tried this code with a script that is attached to my explosion particle system prefab, which is instantiated at the player's position when you die.
Thanks!
UPDATE:
Turns out I had tried using particleSystem.Simulate(Time.unscaledDeltaTime, true, false);, but I was calling it in FixedUpdate() rather than Update().
However, the Asteroid Base post below posted by Garfty is really interesting and is probably worth doing in the long run!
One way you could do it is by using Time.unscaledDeltaTime
Another way you could approach something like this is by creating your own time manager, but it requires some discipline to stick to. The people at Asteroid Base wrote a nice article on something like this here.
I hope this helps!
Related
i have an example : player object, obstacle_1 object and obstacle_2 object. How do I check which one player collides with? I mean I want to do script_1 for colliding player with obstacle_1 and do script_2 when player collides with obstacle_2. Example:
private void OnTriggerEnter2D(Collider2D collision)
{
//script 1 when this object(player) collides with obstacle_1;
//script 2 when this object(player) collides with obstacle_2;
}
It seems like you're asking how to differentiate between objects. There are many ways of doing so, and it's hard to say which will work best for you since you didn't give a lot of context. Here are some ideas:
One of the simplest ways would be comparing the object's names:
private void OnTriggerEnter2D(Collider2D collision)
{
if(collision.gameObject.name == "Obstacle_1"){
//React to obstacle 1
}
else if (collision.gameObject.name == "Obstacle_2"){
//React to obstacle 2
}
}
I really wouldn't encourage this though, since the name of the objects can be changed quite easily.
You could add Tags to obstacle_1 and obstacle_2, and compare tags, like:
private void OnTriggerEnter2D(Collider2D collision)
{
if(collision.gameObject.CompareTag("Obstacle 1")){
//React to obstacle 1
}
else if (collision.gameObject.CompareTag("Obstacle 2")){
//React to obstacle 2
}
}
This is a bit better, since changing tags is not as easy as changing names, but there's still room for spelling errors. Comparing strings is not very efficient either, especially if done frequently.
You could create different components for obstacle_1 (BouncyObstacle) and obstacle_2 (SpikeObstacle), and then have your player try to get these components, like:
private void OnTriggerEnter2D(Collider2D collision) {
if(collision.gameObject.TryGetComponent(out BouncyObstacle bouncyObs){
// React to bouncy obstacle
}
else if(collision.gameObject.TryGetComponent(out SpikeObstacle spikeObs)
{
//React to spike obstacle
}
}
This is a little bit better, but every time you want to add a new obstacle, you need to add an if to your player code.
Again, it's hard to say which solution would be best. From your snippet, it seems like the player class is responsible for reacting to the obstacles, and this means that every time you want to add a new obstacle, you need to open the player class and change the OnTriggerEnter method. Perhaps it would be better to move this responsibility to separate components. That way you'll be able to add as many obstacles you want without changing any of the previous classes you made. Something like:
public class BouncyObstacle : MonoBehaviour
{
void OnTriggerEnter2D(Collider2D other)
{
if(other.TryGetComponent(out Player player))
{
player.Bounce();
}
}
}
public class SpikeObstacle : MonoBehaviour
{
void OnTriggerEnter2D(Collider2D other)
{
if(other.TryGetComponent(out Player player))
{
player.Kill();
}
}
}
// etc..
I am trying to display ads from pubcetner in XNA game for WP. I am using DrawableAd class as it is written here:
http://msdn.microsoft.com/en-us/library/advertising-mobile-windows-phone-xna-silverlight-hybrid-walkthrough-ads%28v=msads.20%29.aspx
But I can't see any ads rendering. Is it works? Or is there any other way to display ads? I wanted to try Adrotator, but I can't
get NuGet Package for this
Ok, I had a lot of problems with this in my game.
Without seeing your code, it's hard to actually diagnose, so without posting that from your Game.cs (assuming it's in Game.cs), you will need to make sure to test the following things.
//Global variable area
DrawableAd bannerAd;
string applicationID = "test_client"; //Or insert your actual ID. I recommend you first do with test client
//ie. 1234567-d123-1234-a12b-1a23b4567890
string adUnitID = "Image480_80"; //Or the actual unit ID. ie. 123456
//Initialization
protected override void Initialize(){
AdGameComponent.Initialize(this, applicationId);
AdGameComponent.Current.CountryOrRegion = System.Globalization.RegionInfo.CurrentRegion.TwoLetterISORegionName;
bannerAd = AdGameComponent.Current.CreateAd(adUnitId, new Rectangle(0,400, 480, 80), true);
AdGameComponent.Current.Enabled = true;
AdGameComponent.Current.Visible = true;
base.Initialize();
}
The part that got me by surprise was that I got rid of my base.Update(...) and base.Draw(...) calls and it actually prevented anything from ever being updated or called with the Ad. I also remember through my endeavor that the region was apparently important in some cases. Can't find the source for that. You might be able to get rid of it.
protected override void Update(GameTime gameTime)
{
//Your code here...
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
//Your code here...
base.Draw(gameTime);
}
Hope that helps! Good luck. I know I had a pretty nasty time figuring out why mine didn't work. Make sure your render order is correct to. I'm not sure, but perhaps you might be drawing everything ON the ad. I haven't looked into whether you could actually do that or if the depth field of the ad was always on top. I just leave my base.Draw(gameTime) at the bottom of everything else.
so I'm developing a game for android in LibGDX and I've stumbled upon a problem: I have a scene with an image in it and I want to be able to click/touch the image and make stuff happen after doing so.
I've been trying to google a solution for the past day but I keep on missing something vital. Here's my code:
public class ScreenSplash implements Screen {
private Texture textureGlobe = new Texture(Gdx.files.internal("graphics/splash.png"));
private Image imageGlobe = new Image((new TextureRegion(textureGlobe)));
public ScreenSplash() {
imageGlobe.addListener(new InputListener() {
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
Gdx.app.log(Game.LOG, "image clicked");
return true;
}
});
stageGame.addActor(imageGlobe);
}
...
}
I've also heard that I'm supposed to put this somewhere:
Gdx.input.setInputProcessor(inputProcessor);
But I don't really know what to do with it.
Your Stage is your InputProcessor so do something like Gdx.input.setInputProcessor(stageGame);. The Stage will route the events to the actors.
Ah, I've found out what the problem was.
I imported java.awt.event.InputEvent instead of com.badlogic.gdx.scenes.scene2d.InputEvent and the touchDown function wasn't properly overriden because of this.
I am using this example:
Android - Setting a Timeout for an AsyncTask?
in the following way:
al.setOnClickListener(new OnClickListener(){
public void onClick(View w)
{
final AlogLoader loader = new AlogLoader();
loader.execute();
Handler handler = new Handler();
handler.postDelayed(new Runnable()
{
public void run()
{
if(loader.getStatus() == AsyncTask.Status.RUNNING)
{
loader.cancel(true);
}
}
}, 1);
}
});
I set it to "1" because I wanted to see if I can stop it practically before it even starts - to see if my handler is working - in reality I would probably set it to 15000 (15 seconds).
However what happens is confusing:
Running the application, causes my onPreExecute() to draw a loading screen, which doesn't ever exit, so the user just sees a loading screen perpetually.
Running it in the debugger with the breakpoint at loader.cancel(true) -> Causes the debugger to stop at that line, which is expected because its only allowed to run for 1 millisecond. However, when I hit the resume button in the debugger after that - my onPostExecute() is called... How is that possible?
Clearly, I'm very new to timing out asynctasks - after some research, I found the example above and it seemed to make the most sense to me, definitely more then
loader.get(15000, TimeUnit.MILLISECONDS);, since that blocks the UI Thread.
Any help is appreciated...even an explanation on the process..
Okay,
So I essentially solved my problem but I am still confused... Don't know if that's all good.
I was unclear on how loader.cancel(true) actually works. Based on the API level (read this question : AsyncTask.onCancelled() not being called after cancel(true)) you need to have an onCancelled() or onCancelled(params) method, or both in your AsyncTask.
I did not have this, so this explains why I saw a loading screen forever. Okay Good.
Yet, it doesn't explain how in the debugger, I managed to still call the onPostExecute(), because according to Android API's, onPostExecute is never called once cancel(true) has been called...
The answer is:
Add this to your AsyncTask-
#Override
protected void onCancelled()
{
Toast.makeText(FriendsActivity.this,"Blah- reason", Toast.LENGTH_LONG).show();
loadingScreen.dismiss();
}
I'm setting out to create a weather model display tool (web application), and from what I've seen, I'm really liking the idea of using Google Web Tools, and especially the SmartGWT toolkit. My one biggest sticking point at this point is finding some way to create a sort of image "looper" (displaying all images in a particular "set" one after another, not unlike a slide show). For reference, I need functionality (at least on a basic level) similar to this: http://rapidrefresh.noaa.gov/hrrrconus/jsloop.cgi?dsKeys=hrrr:&runTime=2012053007&plotName=cref_sfc&fcstInc=60&numFcsts=16&model=hrrr&ptitle=HRRR%20Model%20Fields%20-%20Experimental&maxFcstLen=15&fcstStrLen=-1&resizePlot=1&domain=full&wjet=1 (though it certainly need not be exactly like that).
Does anyone know of (ideally) some sort of GWT module that can do image looping? Or if not, does it sound like something an intermediate programmer could figure out without too much trouble (I'm willing to accept a challenge), even if I've never explicitly used GWT before? I'm sure I could whip together something that pulls each image in as it goes through a loop, but prefetching them would be even more ideal.
Please comment if you need clarification on anything!
As far as I'm aware there's not a pre-fab solution to do this, although maybe SmartGWT has something I don't know about. In any case, it won't be too hard to roll your own. Here's some code to get you started:
public class ImageLooper extends Composite {
// List of images that we will loop through
private final String[] imageUrls;
// Index of the image currently being displayed
private int currentImage = 0;
// The image element that will be displayed to the user
private final Image image = new Image();
// The Timer provides a means to execute arbitrary
// code after a delay or at regular intervals
private final Timer imageUpdateTimer = new Timer() {
public void run() {
currentImage = (currentImage + 1) % images.length;
image.setUrl(imageUrls[currentImage]);
}
}
// Constructor. I'll leave it to you how you're going to
// build your list of image urls.
public ImageLooper(String[] imageUrls) {
this.imageUrls = imageUrls;
// Prefetching the list of images.
for (String url : imageUrls)
Image.prefetch(url);
// Start by displaying the first image.
image.setUrl(imageUrls[0]);
// Initialize this Composite on the image. That means
// you can attach this ImageLooper to the page like you
// would any other Widget and it will show up as the
// image.
initWidget(image);
}
// Call this method to start the animation
public void playAnimation() {
// Update the image every two seconds
imageUpdateTimer.scheduleRepeating(2000);
}
// Call this method to stop the animation
public void stopAnimation() {
imageUpdateTimer.cancel();
}
}
One annoying thing with this implementation is that you have no way of knowing when your list of images has finished loading; Image.prefetch doesn't have a callback to help you here.