Libgdx - How to spawn particles only when I hold mouse button? - system

So I slowly got to know how to manipulate particle system and emitter in-game through the code, but there is one simple task I can't get to know how... How can I spawn particles ONLY when I hold the mouse button? I tried a work-around by setting the maxCount of emmiter to 0 when its not pressed but then it either doesnt emit particles at all, or just makes the existing ones disappear immidiately, which looks very unnatural and I don't want it. Is there a way to emit them "manually" in render method?

You probably want to do set the Emission scaled value on the particle emitter. You can leave the max count at whatever maximum particle number you want.
To turn off the creation of particles:
emitter.getEmission().setLow(0);
emitter.getEmission().setHigh(0);
To turn it back on:
emitter.getEmission().setLow(10);
emitter.getEmission().setHigh(10);

Try using a Pool combined with your listeners:
gitHub link

Ok this is what I got to make it work. "blowing" is basically a boolean that is true when holding mouse button and false when not.
if (blowing) {
effectEmitter.start();
} else {
effectEmitter.allowCompletion();
}

Related

Godot: How would I get InputEventMouseMotion in the _process function?

I was working on a game in Godot and want to get this:
func _input(event):
if event is InputEventMouseMotion:
pass
in the _process function (without using _input or _unhandled_input or anything related or defining a new function)
Is there a way to do this, and if so, how?
You could use
Input.get_last_mouse_speed()
But this looks tricky to get right. From the official documentation:
Returns the mouse speed for the last time the cursor was moved, and this until the next frame where the mouse moves. This means that even if the mouse is not moving, this function will still return the value of the last motion.
Using the _input function is a better solution. If you want to handle mouse movement in _process, you can use _input to store the movement in a variable, which is then read in _process.
Note that this is only a problem for the motion. You can easily get the state of the mouse buttons from Input (get_mouse_button_mask).

gsap, revert animation after play

I'd like to play an animation with gsap, but after it has finished playing, I want to reset it. Like after playing that animation, reset, after it is finished. Without clicking anywhere or calculating the play time, sleeping or waiting the process. Just simply something like:
TweenLite.to(thing, 1, {x:'-20px'}).reset();
(ideally, there is no reset call in gsap.. how to acieve this?)
I can't get it to work with .seek(), .time(), .pause().
Those always interrupt the animation.
Preferable I want to use a simple TweenLite, or if it has to, a TimelineLite.
Im searching / looking for it since hours..
There are many ways to do it, but here's a simple one:
TweenLite.to(thing, 1, {x:-20, onComplete:function() {
this.pause(0); //seeks the tween (this) to 0 (playhead's starting position) and pauses immediately.
}});
Does that help?

Changing game size in Phaser dynamically

I'm building a game in which I can enter a building, which I'm handling via states. In other words, when my character overlaps with the door, the program starts a new state in which the interior of the building is built. Now, I want the interior to have smaller dimensions than the world, so I want to change the game size when I start this new state.
I tried this:
create: function()
{
//game size was 1000x700, I want to scale it to 700x450
game.width = 700;
game.height = 450;
//rest of creation code...
}
I also tried things like changing camera bounds, world bounds, world size, but method above shows the most promise.
The problem, however, is that the resize for some reason does not show until I click away from my browser tab and back. Calling game.width in the console yields 700 at all times, but it doesn't show it as such until tabbing out and back.
On top of that, the contents of the game (floor, furniture) are scaled down when the game resizes, defeating the purpose. I don't understand why it would, since there's no scaling anywhere in my code.
Any help would be greatly appreciated.
Edit:
I just saw that you were the one asking the question I linked to, so I guess the question is solved :D
I had a similar problem some time ago. As you described yourself: what you are doing is a resize! So the game resizes itself which means the same as re-scaling everything in the game.
if you want to cut the game smaller you can simply use
game.scale.setGameSize(700, 450);.
(see this post if you need more information)
As additional information: I later had problems with cutting the game size equally on all sides, so if you should come to face the same problem, have a look at this post

Find out when SCNNode disappears from SCNScene

Does a particular method get called when an SCNNode is removed from the scene?
-(void)removeFromParentNode;
Does not get called on the SCNNode object.
To set the scene
I am using gravity to pull down an object. When an object goes too far down, it automatically disappears and the draw calls and polygon counts decrease. So the SCNNode is definitely being destroyed, but is there a way I could hook into the destruction?
Other answers covered this pretty well already, but to go a bit further:
First, your node isn't being removed from the scene — its content is passing outside the camera's viewing frustum, which means SceneKit knows it doesn't need to issue draw calls to the GPU to render it. If you enumerate the child nodes of the scene (or of whatever parent contains the nodes you're talking about), you'll see that they're still there. You lose some of the rendering performance cost because SceneKit doesn't need to issue draw calls for stuff that it knows won't be visible in the frame.
(As noted in Tanguy's answer, this may be because of your zFar setting. Or it may not — it depends which direction the nodes are falling out of camera in.)
But if you keep adding nodes and letting physics drop them off the screen, you'll accumulate a pre-render performance cost, as SceneKit has to walk the scene graph every frame and figure out which nodes it'll need to issue draw calls for. This cost is pretty small for each node, but it could eventually add up to something you don't want to deal with.
And since you want to have something happen when the node falls out of frame anyway, you just need to find a good opportunity to both deal with that and clean up the disappearing node.
So where to do that? Well, you have a few options. As has been noted, you could put something into the render loop to check the visibility of every node on every frame:
- (void)renderer:(id<SCNSceneRenderer>)renderer didSimulatePhysicsAtTime:(NSTimeInterval)time {
if (![renderer isNodeInsideFrustum:myNode withPointOfView:renderer.pointOfView]) {
// it's gone, remove it from scene
}
}
But that's a somewhat expensive check to be running on every frame (remember, you're targeting 30 or 60 fps here). A better way might be to let the physics system help you:
Create a node with an SCNBox geometry that's big enough to "catch" everything that falls off the screen.
Give that node a static physics body, and set up the category and collision bit masks so that your falling nodes will collide with it.
Position that node just outside of the viewing frustum so that your falling objects hit it soon after they fall out of view.
Implement a contact delegate method to destroy the falling nodes:
- (void)physicsWorld:(SCNPhysicsWorld *)world didBeginContact:(SCNPhysicsContact *)contact {
if (/* sort out which node is which */) {
[fallingNode removeFromParentNode];
// ... and do whatever else you want to do when it falls offscreen.
}
}
Your object will disappear if it goes further than the ZFar property of your active camera. (default value is 100.0)
As said by David Rönnqvist in comments, your Node is not destroyed and you can still modify its property.
If you want to hook-up to your Node's geometry disappearance, you can calculate the distance between your active camera and your Node and check it every frame in your rendering loop to trigger an action if it gets higher than 100.
If you want to render your Node at a greater distance, you can just modify the ZFar property of your camera.

How would I go about creating levels in a game that scroll automatically?

I'm working on a 2D shmup, and the idea is that the level continuously scrolls automatically, and your character can move around the screen.
Now, I'm having trouble figuring out how I would implement this and Google hasn't been any help. Right now I have a scrolling background (the background position is simply decremented for each frame) and the player can move around freely in the window, but how would I go about creating the objects in the level? Would I just use a timer to trigger objects and enemies or is there a way to do it based on the position/width of the background (I'd prefer the second method...But I have no clue how that would be done)?
Since this is a general question and doesn't really pertain to any of my code that I've already written as far as I know, I don't think I need to include any of it...But I'll be happy to provide any part of it if needed.
I'd recommend either:
Use physical triggers
Use a list of timed events
Physical triggers
Simply place a box on your level. When it scrolls partially or completely onto the screen (whichever makes more sense - maybe use both in different cases?), you trigger the event associated with that trigger.
This would be simpler to support in a level editor because the physical nature is inherently very easy to visualize.
Timed events
You basically create a timer object at the beginning of the level, and an ordered queue of events. In your game update loop, peek at the head of the queue. If the trigger time of the item at the head of the queue is less than the current elapsed time, pop the item off the queue and trigger the event.
Timed events would be more generically useful because it would also support non-scrolling level, or non-scrolling portions of levels.
Combination of both
You could also do some sort of combination of these to get the benefits of both styles: Easier visualization/level editing, and supporting non-scrolling sections or time-based events.
Each physical trigger will have its own script queue. When the trigger is hit, a timer is started and an event queue is created. That timer and queue is added to a list of currently running timers and queues.
In your update function, you check all items on the list, and trigger events the same way you did with the timed event queue above. Once a queue is emptied, you remove it from the list of timers/queues.
How to detect that a trigger is on-screen
You should implement scrolling first.
One you have scrolling, calculate the rectangle that matches where the screen is located in your pixel/world coordinate system. This will give you the "bounding box" of the screen.
From here, do an intersection test between your event trigger's "bounding box" and the screen.
Here is a test to see if there is any overlap between two rectangles. It isn't order-specific:
rect1.left < rect2.right && rect1.right > rect2.left
&& rect1.top < rect2.bottom && rect1.bottom > rect2.top
If the rects are touching at all, it will return true.
Here is a test to make sure rect1 contains rect2. Order is important:
rect1.left <= rect2.left && rect1.right >= rect2.right
&& rect1.top <= rect2.top && rect1.bottom >= rect2.bottom
If rect2 is completely contained by rect1 (it is completely on-screen), it will return true.
How to implement simple timers
Simply get some sort of clock value (could be SDL_GetTicks), and store that value.
To see how long has elapsed since that timer was started, call the function again and subtract. Compare the values with < to see if the difference is greater than the target time.
Unfortunately, this is where you should use pointers. Something along the lines of:
vector<BadGuy*> Listofbaddies;
//Place enemy just off screen
newYposition = SCREEN_HEIGHT + 20;
//an infinite (almost) amount of badguys can be created with this code:
Listofbaddies.push_back(new Badguy(newXposition,
newYposition,
EnemyType,
blahblah);
Which means that the badguy will need a constructor like:
Badguy::Badguy(float newX, float newY, string Type, whateverelseyouwant){
actualSpritePartOfBadguyClass.setPosition(newX, newY);
}
Does that make sense? I'll elaborate if you ask :D
I'm making a game now that uses something similar :)