How do you Automatically Start Another Background Song in Processing? - processing

When the song (the age of consent) ends how do I start another one after it ends? This is what I have so far.
import processing.sound.*;
SoundFile file;
void setup() {
file = new SoundFile(this, "neworder.mp3");
file.play();
file.amp(0.25);
}

The setup() method is only called once in processing. When processing starts running it calls an inbuilt draw() method continuously in a loop.
its in here you would need to check if the song has finished and load a new one.
void draw() {
background(200, 100, 50,255);
// YOUR CODE HERE TO CHECK IF SONG HAS FINISHED AND PLAY A BETTER SONG LIKE PO BOYDs - BLUE THREE ;)
}

Unfortunately the processing.org SoundFile reference is ommiting a few helpful functionalities of the class like percent() or position()
You could so something like:
import processing.sound.*;
SoundFile file;
void setup() {
file = new SoundFile(this, "neworder.mp3");
file.play();
file.amp(0.25);
}
void draw(){
if(file.percent() >= 99.8){
println("track pretty close to done: cue out / cross fade here");
file.stop();
}
background(0);
text(file.percent(),5,15);
}
You can then either load/play another track or better yet:
load two tracks in setup() but play only one
when the first track is close to being done, start the second track at 0 volume
use lerp() to interpolate it's volume of the first track down and the inverted volume value to bump up the second track with a nice cross fade
Have fun!

Related

How can I randomize a video to play after another by pressing a key on Processing?

I'm quite new to Processing.
I'm trying to make Processing randomly play a video after I clear the screen by mouseclick, so I create an array that contain 3 videos and play one at a time.
Holding 'Spacebar' will play a video and release it will stop the video. Mouseclick will clear the screen to an image. The question is how can it randomize to another video if I press spacebar again after clear the screen.
I've been searching all over the internet but couldn't find any solution for my coding or if my logic is wrong, please help me.
Here's my code.
int value = 0;
PImage photo;
import processing.video.*;
int n = 3; //number of videos
float vidN = random(0, n+1);
int x = int (vidN);
Movie[] video = new Movie[3];
//int rand = 0;
int index = 0;
void setup() {
size(800, 500);
frameRate(30);
video = new Movie[3];
video[0] = new Movie (this, "01.mp4");
video[1] = new Movie (this, "02.mp4");
video[2] = new Movie (this, "03.mp4");
photo = loadImage("1.jpg");
}
void draw() {
}
void movieEvent(Movie video) {
video.read();
}
void keyPressed() {
if (key == ' ') {
image(video[x], 0, 0);
video[x].play();
}
}
void mouseClicked() {
if (value == 0) {
video[x].jump(0);
video[x].stop();
background(0);
image(photo, 0, 0);
}
}
You have this bit of logic in your code which picks a random integer:
float vidN = random(0, n+1);
int x = int (vidN);
In theory, if you want to randomise to another video when the spacebar is pressed again you can re-use this bit of logic:
void keyPressed() {
if (key == ' ') {
x = int(random(n+1));
image(video[x], 0, 0);
video[x].play();
}
}
(Above I've used shorthand of the two lines declaring vidN and x, but the logic is the same. If the logic is harder to follow since two operations on the same line (picking a random float between 0,n+1 and rounding down to an integer value), feel free to expand back to two lines: readability is more important).
As side notes, these bit of logic look a bit off:
the if (value == 0) condition will always be true since value never changes, making both value and the condition redundant. (Perhaps you plan to use for something else later ? If so, you could save separate sketches, but start with the simplest version and exclude anything you don't need, otherwise, in general, remove any bit of code you don't need. It will be easier to read, follow and change.)
Currently your logic says that whenever you click current video resets to the start and stops playing and when you hit the spacebar. Once you add the logic to randomise the video that the most recent frame of the current video (just randomised) will display (image(video[x], 0, 0);), then that video will play. Unless you click to stop the current video, previously started videos (via play()) will play in the background (e.g. if they have audio you'll hear them in the background even if you only see one static frame from the last time space was pressed).
Maybe this is the behaviour you want ? You've explained a localised section of what you want to achieve, but not overall what the whole of the program you posted should do. That would help others provide suggestions regarding logic.
In general, try to break the problem down to simple steps that you can test in isolation. Once you've found a solid solution for each part, you can add each part into a main sketch one at a time, testing each time you add something. (This way if something goes wrong it's easy to isolate/fix).
Kevin Workman's How To Program is a great article on this.
As a mental excercise it will help to read through the code line by line and imagine what it might do. Then run it and see if the code behaves as you predicted/intended. Slowly and surely this will get better and better. Have fun learning!

Why does this Processing program starting with PVector cause an error?

PVector m3()
{
return null;
}
(yup, that's the whole program) gives
Error on "PVector"
Why? It looks legal to me.
The same fail occurs with a different Processing-specific type e.g. color but not with a native type e.g. float.
Workaround:
This is because PVector is a class, not a method. I think if you want to make it return null, you have to add void in the front.
void PVector m3() {
return null;
}
Unless you want to create a PVector, you simply type it like creating a new object. Here are some examples:
PVector m3 = null;
PVector m1 = new PVector();
PVector m2 = new PVector(2, 3);
For more information on how to use PVector, I suggest you to look at the information posted on the official Processing website. Here is the link: https://processing.org/reference/PVector.html
I hope this answers your question, good luck!
Processing works in two modes:
Static mode is just a bunch of function calls. In this mode, Processing just draws a single image and then stops. Here's an example:
background(32);
ellipse(10, 20, 50, 50);
Active mode is a sketch that contains functions like setup() and draw(). In this mode, Processing continues executing code after the program starts: for example it executes draw() 60 times per second, or mousePressed() when the user presses the mouse. Here's an example:
void draw(){
background(32);
ellipse(mouseX, mouseY, 25, 25);
}
The problem with your sketch is that Processing doesn't know which mode you're trying to use. It sees that you don't have a setup() or draw() function (or any other Processing callback function), so it thinks you're trying to use static mode. But then it sees you've defined a non-callback function, so it doesn't know how to work.
Like you've discovered, the solution to your problem is to add other functions, so Processing can know what mode you want to be in. Also note that none of your code makes a ton of sense by itself, because Processing has no way of accessing it. My guess is you're planning on adding setup() and draw() functions eventually, so just add them now to get rid of your error.
For more info:
See George's answer here.
See my answer here.
See this GitHub issue where the creator of Processing explains all of the above.

Unity2d game shooting and animation sync issue

I'm a new in Unity and making my first 2D game. I seen several topics on this forum in this issue, but I didn't found the solution.
So I have a lovely shooting animation and the bullet generation. My problem, I have to generate the bullet somewhere at the middle of the animation, but the character shoots the bullet and the animation at the same time, which killing the UX :)
I attached an image, about the issue, this is the moment, when the bullet should be initialized, but as you can see it's already on it's way.
Please find my code:
The GameManager update method calls the attackEnemy function:
public void Awake(){
animator = GetComponent ();
animator.SetTrigger ("enemyIdle");
}
//if the enemy pass this point, they stop shooting, and just go off the scren
private float shootingStopLimit = -6f;
public override void attackPlayer(){
//animator.SetTrigger ("enemyIdle");
if (!isAttacking && gameObject.transform.position.y > shootingStopLimit) {
isAttacking = true;
animator.SetTrigger("enemyShoot");
StartCoroutine(doWait());
gameObject.GetComponentInChildren ().fireBullet ();
StartCoroutine (Reload ());
}
}
private IEnumerator doWait(){
yield return new WaitForSeconds(5);
}
private IEnumerator Reload(){
animator.SetTrigger ("enemyIdle");
int reloadTime = Random.Range (4,7);
yield return new WaitForSeconds(reloadTime);
isAttacking = false;
}......
My questions:
- How can I sync the animation and the bullet generation ?
Why not the doWait() works ? :)
Is it okay to call the attackPlayer method from the GameManager update ?
The enemies are flynig from the right side of the screen to the left, when they reach the most right side of the screen, they became visible to the user. I don't know why, but they to a shooting animation (no bullet generation happen )first, only after it they do the idle. Any idea why ?
Thanks,
K
I would suggest checking out animation events. Using animation events, you can call a method to instantiate your bullet.
To use Mecanim Animation Events you need to write the name of the function you want to call at the selected frame in the "Function" area of the "Edit Animation Event" window.
The other boxes are for any variables that you want to pass to that function to trigger whatever you have in mind.
Triggering/blending between different animations can be done in many different ways. The event area is more for other things that you want to trigger that are not related to animation (e.g. audio, particle fx, etc).

StateTime in LibGDX Animation

How do I use implemented Animation in LibGDX? I know, that the documentation can be found here, but when I want to get a frame out of the Animation, I need to use stateTime, which isn't explained anywhere in the documentation. So the question is, what is stateTime in terms of LibGDX's Animation?
There is some more doc on the getKeyFrame method documentation:
Returns a TextureRegion based on the so called state time. This is the amount of seconds an object has spent in the state this Animation instance represents, e.g. running, jumping and so on.
(This documentation doesn't really make any sense to me either.)
But, the Animation.java source is readable. It looks like it boils down to
getKeyFrameIndex
which divides the stateTime by frameDuration (which is a parameter of the constructor -- how long each frame lasts) to compute an array index. getKeyFrameIndex does different things for looping or non-looping sequences, but basically it takes the array index to look up the right key frame in the sequence to display.
So the "stateTime" is the input to pick a key frame from your Animation. The documentation is assuming you have one Animation instance for "running" and another for "jumping" (these are the "states" its referring to). To find the right key frame within an Animation, you tell it how long you've been in this "state". So, if you've been in the "running" Animation instance for 1.2 seconds, it does some math to figure out which key frame to show (say you've initialized the instance with 30 frames that show for 0.0333 seconds and loop -- it picks the 6th frame).
The wiki https://github.com/libgdx/libgdx/wiki/2D-Animation has some more details and an example, but doesn't address this directly, either.
animationFrames = walkSheetArray[moveDirection];
animation = new Animation(1f / 5f, animationFrames);
myAnimatedActor = new AnimatedActor(animation);
stage.addActor(myAnimatedActor);
public class AnimatedActor extends Image {
private float stateTime = 0;
Animation animation;
public AnimatedActor(Animation animation) {
super(animation.getKeyFrame(0));
this.animation = animation;
}
#Override
public void act(float delta) {
((TextureRegionDrawable) getDrawable()).setRegion(animation.getKeyFrame(stateTime += delta, true));
super.act(delta);
}
}

reset game in processing

I am making a Tron game in Processing. I have the game all worked out but I do not know how to add a reset option to start a new game after the player loses.
Anyone have some suggestions?
Well usually you should make a method that will reset/recreate/delete what is needed to restart your game. Like(pseudo):
void reset(){
score = 0;
ballsList.removeAll();
playerPositionX = 0;
playerPositionY = 0;
}
And then call it when needed.
Avoid using "init" as name of the method or you will override a built in method.
Wouldn't a simple switch case work fine ?
Switch (levels):
Case one:
Case last level:
If (this == that){
levels = one;
break
}
What I would say is wrap your whole gamecode in a function like void inGame(){gamecodeing} and when something happens like if (player.state == "dead"){inGame();} and the ingame at the starting as well. Like so:
void setup() {
size(500,500);
}
void draw() {
inGame();
if (playerHasLost) {inGame();}
}
void inGame() {gameStuff}
and every time inGame() is called it kind of does it all over again.
I'd reccomend running the setup() again.
And then store your variables in there like x = 0;, score = 0;.

Resources