Titanium: Sounds overlapping each other - audio

Sorry if it looks like a noob question, but i'm new to titanium so some concepts are foreign to me.
I have a script called entry.js which is called from details.js using require('entry.js').
Now within entry.js i play a sound, depending on what entry is selected.
var player = Ti.Media.createSound({url:"/sounds/0"+e.id+".mp3"});
player.play();
That part is ok. The problem is when i go back to the details screen, and select another entry, the new sound overlaps the old one.
So i need a global value or object that i can pass down to entry.js to make sure if the sound is playing or not.
But if i declare a global TI.Media object in details.js i get an error in entry.js:
Cant find variable player
So the question is, how do i detect in a sub javascript file that an instance of audio is already running?

You can check whether the audio is playing using the isPlaying() method or try to stop the playing music when you get back to the details screen. You can use stop() method to stop the sound. It will stops playing the audio and resets the playback position to the beginning of the clip.
If you want to create a global variable or pass variable from one window to another, refer the following links
Passing parameters from currentWindow to the new window in Titanium
Passing variable between windows

Related

What is the ideal way to change the value of a progress bar with on-screen button press (Godot)

I'm new to coding and Godot and I need help with changing the value of my progress bar from the script inside my button node. The error I get is 'get_node: (Node not found: "/root/ProgressBar" (absolute path attempted from "/root/Node2D/Button").)' I want every time the button is '_pressed()' to increment health by 10 which would change 'value' in ProgressBar to that value.
extends Button
var health= 0
func _ready():
pass
func _process(delta):
pass
func _pressed():
health += 10
print(health)
get_node("/root/ProgressBar").set_value(health)
The path /root/ProgressBar is unlikely to be correct. Usually, as a child of root, you would have the current scene, and as child of that something else (So the path could be something like /root/MySceneName/ProgressBar). And relative paths work too… Usually, we would solve this by means of figuring out the correct path…
But things have changed (For Godot 3.5 and newer). What you are going to do is open the script while the scene is open, and then drag - with CTRL pressed - the Control you want to access (i.e. the ProgressBar) on script, outside of any method (func).
By doing that Godot will generate a line of code that looks something like this:
onready var progress_bar: ProgressBar = $"Path/To/ProgressBar"
In other words, Godot will figure out the path you need, and set up a variable you can use. Godot will set the variable to the Node you dragged as part of the initialization of you script. Assuming all goes well.
You might get an error saying that the script is not used in the scene. Perhaps you have the wrong script open, or you use the script in multiple places and Godot got confused… Either way, close the script and open it from the scene, and it should work.
And, of course, you can use the variable to set its properties, for example:
progress_bar.value = health
You might also be interested in scene unique names. On the contextual menu of the Nodes in the Scene panel you can select "% Access As Scene Unique Name", this will allow you to access the Node with this syntax:
$"%ProgressBar"
Regardless of where it is in the scene tree. Except, be aware that the name must be unique for that scene (you won't be able to do that with two Nodes that have the same name).
This has the advantage that if later you change where the Node is in the scene tree (for example to add some Container to organize your Controls), you don't have to update the path to where you use it. Instead it should continue to work, as long as you don't change the name.
Let that be yet another reason to pick good Node names.
And yes, dragging the Node to the script also works with these.
The problem with your path is, that your missing the name of your main node in it.
So let's say your node tree looks like this:
MainNode
ProgressBar
Button
To get the ProgressBar from anywhere you would need to use get_node("/root/MainNode/ProgressBar")

Area2D Not Triggering Object in Godot

Here is my code: (just so you know I am a beginner and I just started this week, though I do have knowledge with other languages and game engines)
func _on_Area2D_area_entered(area):
get_parent().get_node("Level 1/Area2D/Flag").rotation_degrees += 1
What I was trying to accomplish was that the Player GameObject would see if its in the area of the Flag, and if it is, the flag would rotate.
I am not sure where the issue is. I think it is probably in the second line. I provided a screenshot below if I did the setup wrong. I have looked at the other close questions asked on the same topic, but they did not answer my question.
The "Player" GameObject is the one with the script that contains the detection if its in the area2D.
If you want to check if the Area2D is positioned correctly during runtime enable Debug -> Visible Collision Shapes.
If you want to check if _on_Area2D_area_entered is running, add breakpoints (or use print).
Did you get an error?
If there isn't a Node there, this expression will cause an error in runtime:
get_parent().get_node("Level 1/Area2D/Flag")
If you want to be able to check, you can use get_node_or_null and is_instance_valid.
Since you didn't mention any error, I'm going to guess the method is not running.
If the method is not running, the most likely culprit is that - I'm guessing given then name of the method - you connected the "area_entered" signal but intended to connect the "body_entered" signal.
The "area_entered" signal will trigger when another Area2D enters the Area2D. But I only see one Area2D in your scene tree. On the other hand the "body_entered" will trigger when a PhysicsBody2D (e.g StaticBody2D, KinematicBody2D, RigidBody2D) enters the Area2D. In either case you get what entered as a parameter of the method.
Other reasons why the Area2D might not be detecting what you want include no intersection of collision_layer and collision_mask and monitoring being disabled.
And to dismiss a couple possible misconceptions:
The "area_entered" and "body_entered" trigger when the Area2D or PhysicsBody2D respectively enter the Area2D, not every frame they are inside. So rotation_degrees += 1 is not a rotation animation.
You will get notifications of anything that trigger the signals, not just the object to which you connected it. You may have to further filter, e.g. if body == self:.
For people arriving here from search, I want to link a similar case: Enemy is not affected by bullets. And also my full explanation of how to set up physic nodes.

posenet p5js position stops updating when I move to another tab

I'm coding with posenet and p5js. I made a grid on my screen which plays different audio fragments on different x ranges. This works very well. However, if I keep it running and open another tab in my chrome webbrowser, the x position gets stuck in one area (it doesn't update anymore) and the same audio fragment is repeated until you get crazy.
Is there a way I can prevent this?
Well, some code would be great to be honest, but variables shouldn't really get stuck if you open another tab and so on, are you sure you haven't just missssspelled some variable, or some function or did one of those random mistakes?
If it truly is the p5.js I guess just try checking everytime draw fires if your canvas is still focused.
I believe you can also have canvas.hasFocus... exept you'd have to declare createCanvas as a variable (cause canvas is basically just a button or a textArea to html and javascript and p5)
Also I think there's a command to only allow for one audio channel and stuff in js and p5...
function draw() {
if(document.hasFocus()){
// all of your other code even
// background() and stuff...
}
}

How do I stop a music track from looping in Libgdx

In my project I am attempting to play a music track when the screen switches to the game over screen. It starts when it is supposed to, however, the track continuously loops even when looping is set to false. I tried also using the OnCompletionListener, However I either set it up incorrectly or It just didnt work for some reason or another
This is the variable in my create method.
music = Gdx.audio.newMusic(Gdx.files.internal("gameover.mp3"));
This is the track set up in the render method
music.setVolume(.2f);
music.setLooping(false);
music.play();
The problem seems to be you're calling play() in the render() method, try calling it on the show() method or use a boolean to flag that you're already playing the music and don't play it again.

Spotify API: Playing a specific part of a song

I'm trying to play a song from a position other than the beginning of the track.
I've found that player.position = X will work as a standalone function, such as triggered by a button, but triggering it on player.observe(models.EVENT.CHANGE... results in a crash.
I figure this is because the track hasn't actually started playing once the event occurs as the player.position may still be at the previous song's position.
I've also experimented with sp.trackPlayer.seek() which seems to give the same results.
So has anyone got a song to start playing at another position than 0? (Or made the playhead jump ahead after the event of playing a new song?)
Add the position after the uri, for example:
var uri= 'spotify:track:xxxxxx#02:04'
player.playTrackFromUri(uri)
See this sourcecode if you want inspiration:
https://github.com/Iteam1337/Wejay/blob/master/javascript/room.js

Resources