Flutter Flame Game Unit Test Not Working: How do I fix my test case below so that I can test tap events for flame components within my flame game? - flutter

Here is the development test I am trying to run:
testWithGame<ExampleGame>("back button can be tapped", ExampleGame.new,
(game) async {
await game.ready();
final Button component = game.backButton!;
int backButtonPointerID = game.children.length - 3;
game.onTapDown(
backButtonPointerID,
createTapDownEvent(game,
globalPosition:
Offset(component.position.x, component.position.y)));
expect(component.tapped, true);
});
Any thoughts as to why this isn't registering a tap? It should evaluate to true but evaluates to false. When playing the game itself it registers as true, but the unit test doesn't...

The button is most likely not added to the game yet, but will be added in the next tick (if you add it properly, it would help to have that code in the question too).
Try to run game.update after game.ready, or add await to the add call if you are doing it in the game's onLoad method.

Related

Q: How to play OneShotAnimations in reverse with new rive

I have an app where I have an animated check mark made in rive. It is a one shot animation and I want to play it and have it stay in the end of the animation once it's done and then also be able to reverse it when the user clicks on it again. I tried using a SimpleAnimation but I also wasn't able to achieve what I want but it did stay after the animation was done. I don't, however, know how to play an animation and then reverse it or play another animation.
The check mark looks something like this: https://cln.sh/MwLFNs
They are two separate animations but they are copy pasted but in reverse so I can use either one or both.
What I need is to have the animation play and then when the user clicks on the check, I want the animation to change to another one which would then play. How can I achieve this?
Thanks for the help!
you can use State machines, get the state and decide what to do when you want
// Getting state machine
void _onRiveInit(Artboard artboard) async {
final controller =
StateMachineController.fromArtboard(artboard, 'State Machine 1');
if (controller != null) {
artboard.addController(controller);
_lick = controller.findInput<bool>('Lick') as SMITrigger;
}
}
void _onHandlerStateMachine() => _lick?.value = !_lick!.value;
_onRiveInit is a function that is fired when the Rive file is being loading,
after you get the controller to the state machine to modify his state (This is important the name of the State Machine has to be the same of the animation State machine name)
and to modified the value of the trigger your must get the reference on this way
_lick = controller.findInput('Lick') as SMITrigger;
then you can use
void _onHandlerStateMachine() => _lick?.value = !_lick!.value;
to put or remove the animation on any time, also you can have more than one trigger.
If your need that your animation do something and after a time do something else you can use a debouncer function
_debouncer.run(() {
_onHandlerStateMachine()
});
**Remember update the view if are using state management
when the rive file is load
// Loads a Rive file
Future<void> _loadRiveFile() async {
final bytes = await rootBundle.load(riveFileName);
RiveFile rFile = RiveFile.import(bytes);
final artboard = rFile.mainArtboard;
_onRiveInit(artboard);
globalArtboard = artboard
..addController(
_animationController = SimpleAnimation('idle'),
);
}
Here I have and example of a Rive animation of Teddy, using Flutter with provider
https://github.com/alcampospalacios/flutter-rive-animations

Unity timeline, playing animation clips and controlling playback on events

I'm wondering if I can get some help on understanding how to use the timeline
I plan to have the character walking along a path with triggers that will activate the timeline,
I have set up a simple state machine on the monkey
which would be triggered by the path to play the timeline, that part works fine and I can see the events firing
How could I play part of the timeline when triggered (is that even possible?)
where the entire sequence is held Walk>Jump>Walk>Look>Walk>Idle
Or what would I need to do to be able to achieve playback of a clip/or part of a sequence of clips
On the monkey:
public enum State
{
WalkHappy,
Look,
Jump,
}
IEnumerator WalkHappyState()
{
Debug.Log("WalkHappy: Enter");
**//walk sequence with timeline here??**
while (state == State.WalkHappy)
{
yield return 0;
}
Debug.Log("WalkHappy: Exit");
NextState();
}
Called on the paths event listener when triggered
private void OnCustomEvent(string eventname)
{
if (eventname.Contains("MonkeyRunningJumpUp"))
{
GameObject tempMonkey = GameObject.FindGameObjectWithTag("Player");
Monkey tempMonkeyState = tempMonkey.GetComponent<Monkey>();
tempMonkeyState.state = Monkey.State.Jump;
}
}
I have seen its possible to control where the playback starts on a timeline, but I don't know how to force a range/clip to play then pause at the end of the clip rather than the end of the entire sequence
THE question is.. how to stop/pause at end of a clip to resume later?
playableDirector = myPlayableDirectorGO.GetComponent<PlayableDirector>();
Debug.Log(timeLinePosition);
playableDirector.time = timeLinePosition / ((TimelineAsset)playableDirector.playableAsset).editorSettings.fps;
playableDirector.RebuildGraph();
playableDirector.Play();
with thanks Natalie :)
First of all, I recommend to install Default Playables if you haven’t done it yet.
Use Timeline Signals and markers to do something at the end of timeline or at certain point. Also, there is PlayableDirector.stopped event.
You can use pause and resume methods;

(Unity) TextMeshPro score-text doesn´t work on android

I've made a simple relfex-dodging game in Unity in which a TextMeshPro text counts up when an obstacle passes by the player. The obstacle drops from above, falls down and gets destroyed when entering the trigger of a Box-collider. I've attached a Picture of the Scene view.
The "scoreText" variable in the example below is a reference to the TextMeshProUGUI component. When I run the game in Unity (with Unity Remote and my android phone as an input device), the score mechanic works as expected and the text property of the TextMeshProUGUI component changes from 0 to 1, from 1 to 2, and so on. When i hit "build and run" or just "build" in the Build Settings and export the apk-file, the game works well, but the text counts up to 1 and then remains like that. The obstacles are being destroyed, so the else if- block is being executed, too. Nothing changes from the Unity-version to Android, the code stays the same. I tried it on multiple devices, but the Problem stays. There are no errors, the app doesn't crash and I don't know what to do! I would very appreciate if you could help me! Here's the Code:
private void OnTriggerEnter2D(Collider2D other)
{
if (int.Parse(scoreText.text) == 0)
{
scoreText.SetText("1");
}
else if (int.Parse(scoreText.text) > 0)
{
int oldNumber = int.Parse(scoreText.text);
int newNumber = oldNumber + 1;
scoreText.SetText(newNumber.ToString());
}
Destroy(gameObject);
}
Have you tried using scoreText.text = newNumber.ToString()? I personally never use SetText

How to operate two ui button hold down at the same time in Unity [duplicate]

I'm writing a script to put on a button that will detect a drag direction to move a player
void OnGUI()
{
if (buttonRect.Contains(Event.current.mousePosition))
{
if (Event.current.type == EventType.MouseDown)
{
buttonPressed = true;
}
if (Event.current.type == EventType.MouseUp)
{
buttonPressed = false;
}
}
if (buttonPressed && Event.current.type == EventType.MouseDrag)
{
}
}
If this script was to be placed on a button, how could I get the buttons bounds as a rectangle?
Also, If anyone has a better solution for controling movement by drag I would be open for suggestions.
You can implement your own Visual Joystick by using the Unity new event callback functions such as OnBeginDrag OnDrag and the OnEndDrag function. There is a already made Visual Joystick package out there for Unity, so implementing your own is like reinventing the wheel.
All you have to do is to import the CrossPlatformInputManager package from Unity's UnityStandardAssets then use CrossPlatformInputManager.GetAxis("Horizontal") and CrossPlatformInputManager.GetAxisRaw("Horizontal") to read the direction of the image/thumb.
To make one from scratch, you can flow this video tutorial.
If you want a mobile solution that is really accurate and natural like a physical joystick, take a look at this asset:
Ultra Precise D-Pad Joystick

Navigation based on screen orientation (landscape or portrait) in Flash CS6

I'm creating an app that will have a different menu if the phone is held landscape, or portrait.
I figure I have to tell flash to move to a new frame when the phone moves from landscape to portrait or vice versa, but I'm not sure the exact code to after creating the orientation event listener.
There are two ways. Listen for a StageOrientationEvent or listen for Event.RESIZE. I personally prefer to use RESIZE as it is called slightly more often and keeps your interface in sync more.
var landscapeNav:Sprite; // this would be your landscape nav. Obviously does not have to be a Sprite
var portraitNav:Sprite; // same as landscapeNav, but this represents your portrait nav
stage.addEventListener( Event.RESIZE, this.stageResizeHandler );
function stageResizeHandler( e:Event ):void {
if ( stage ) { //just to make sure the stage is loaded in this class so we avoid null refs
if ( stage.stageWidth >= stage.stageHeight ) {
landscapeNav.visible = true;
portraitNav.visible = false;
}
else {
landscapeNav.visible = false;
portraitNav.visible = true;
}
}
}
This could definitely be cleaned up (landscapeNav.visible = stage.stageWidth > stage.stageHeight) but this should give you something to go on. If you want to do an animation as Atriace suggested, you would do a TweenLite/Max call within the conditional in the function instead of setting visible to true/false (after the animation is done, though, you should set visible to false just for the same of optimzation)
You don't need to create a new frame. In fact, it may be more visually appealing to watch the old menu slide off, and the new one to animate in (like with TweenLite).
The documentation on orientation change can be found # Adobe's ActionScript APIs specific to mobile AIR applications: "Screen Orientation", and the API.