Commit 3eab8cc5 authored by Lila NICKLER's avatar Lila NICKLER

Avancement sur deplacement du vaisseau

parent debbb0e7
...@@ -2,6 +2,9 @@ package com.imtm1.starwarsgame; ...@@ -2,6 +2,9 @@ package com.imtm1.starwarsgame;
import android.content.Context; import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity; import android.view.Gravity;
import android.view.MotionEvent; import android.view.MotionEvent;
import android.view.View; import android.view.View;
...@@ -17,11 +20,15 @@ class JoyStickView { ...@@ -17,11 +20,15 @@ class JoyStickView {
int yDelta; int yDelta;
ViewGroup mainLayout ; ViewGroup mainLayout ;
Context context; Context context;
Boolean joystickIsPressed = false;
JoyStickView (ViewGroup mainLayout, Context context)
public JoyStickView (ViewGroup mainLayout, Context context)
{ {
this.mainLayout = mainLayout; this.mainLayout = mainLayout;
this.context = context; this.context = context;
} }
final View.OnTouchListener touchListener = new View.OnTouchListener() { final View.OnTouchListener touchListener = new View.OnTouchListener() {
...@@ -29,28 +36,33 @@ class JoyStickView { ...@@ -29,28 +36,33 @@ class JoyStickView {
public boolean onTouch(View v, MotionEvent event) { public boolean onTouch(View v, MotionEvent event) {
final int x = (int) event.getRawX(); final int x = (int) event.getRawX();
final int y = (int) event.getRawY(); final int y = (int) event.getRawY();
FrameLayout framelayout = new FrameLayout(context); joystickIsPressed = false;
switch (event.getAction()) switch (event.getAction())
{ {
case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_DOWN:
FrameLayout.LayoutParams laParams = (FrameLayout.LayoutParams) v.getLayoutParams(); FrameLayout.LayoutParams laParams = (FrameLayout.LayoutParams) v.getLayoutParams();
xDelta = x - laParams.leftMargin; xDelta = x - laParams.leftMargin;
yDelta = y - laParams.topMargin; yDelta = y - laParams.topMargin;
Log.i("tag", "Hello");
joystickIsPressed = true;
break; break;
case MotionEvent.ACTION_MOVE: case MotionEvent.ACTION_MOVE:
FrameLayout.LayoutParams lParams = (FrameLayout.LayoutParams) v.getLayoutParams(); FrameLayout.LayoutParams lParams = (FrameLayout.LayoutParams) v.getLayoutParams();
lParams.leftMargin = x - xDelta; lParams.leftMargin = x - xDelta;
lParams.topMargin = y - yDelta; lParams.topMargin = y - yDelta;
lParams.rightMargin = 0; lParams.rightMargin = 0;
lParams.bottomMargin = 0; lParams.bottomMargin = 0;
joystickIsPressed = true;
v.setLayoutParams(lParams); v.setLayoutParams(lParams);
break; break;
case MotionEvent.ACTION_UP: case MotionEvent.ACTION_UP:
FrameLayout.LayoutParams layParams = (FrameLayout.LayoutParams) v.getLayoutParams(); FrameLayout.LayoutParams layParams = (FrameLayout.LayoutParams) v.getLayoutParams();
Toast.makeText(context, "I'm here!", Toast.LENGTH_SHORT).show(); //layParams.leftMargin = xDelta;
layParams.gravity = Gravity.CENTER; //layParams.topMargin = yDelta ;
v.setLayoutParams(layParams);
joystickIsPressed = false;
break; break;
default: default:
...@@ -62,6 +74,7 @@ class JoyStickView { ...@@ -62,6 +74,7 @@ class JoyStickView {
} }
}; };
} }
......
...@@ -11,9 +11,11 @@ import android.os.Build; ...@@ -11,9 +11,11 @@ import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.os.Looper; import android.os.Looper;
import android.util.Log;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.view.animation.PathInterpolator; import android.view.animation.PathInterpolator;
import android.widget.FrameLayout;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.RelativeLayout; import android.widget.RelativeLayout;
import android.widget.Toast; import android.widget.Toast;
...@@ -24,6 +26,7 @@ public class MainActivity extends AppCompatActivity { ...@@ -24,6 +26,7 @@ public class MainActivity extends AppCompatActivity {
ImageView padCenter; ImageView padCenter;
ViewGroup mainLayout; ViewGroup mainLayout;
ImageView asteroid1; ImageView asteroid1;
ImageView tie;
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override @Override
...@@ -33,9 +36,9 @@ public class MainActivity extends AppCompatActivity { ...@@ -33,9 +36,9 @@ public class MainActivity extends AppCompatActivity {
padExte = findViewById(R.id.padExte); padExte = findViewById(R.id.padExte);
padCenter = findViewById(R.id.padCenter); padCenter = findViewById(R.id.padCenter);
mainLayout = findViewById(R.id.main); mainLayout = (RelativeLayout) findViewById(R.id.main);
asteroid1 = findViewById(R.id.asteroid1); asteroid1 = findViewById(R.id.asteroid1);
tie = findViewById(R.id.tie);
Path path = new Path(); Path path = new Path();
path.arcTo(0f, 0f, 1000f, 1000f, 270f, -180f, true); path.arcTo(0f, 0f, 1000f, 1000f, 270f, -180f, true);
...@@ -44,18 +47,30 @@ public class MainActivity extends AppCompatActivity { ...@@ -44,18 +47,30 @@ public class MainActivity extends AppCompatActivity {
animator.start(); animator.start();
Runnable runnable = new Runnable() { JoyStickView joyStickView = new JoyStickView(mainLayout,MainActivity.this);
@Override
public void run() {
JoyStickView joyStickView = new JoyStickView(mainLayout,MainActivity.this); Handler handlerForMovingTie = new Handler();
padCenter.setOnTouchListener(joyStickView.touchListener); Runnable movingTie = new Runnable() {
@Override
public void run() {
tie.setTranslationX(joyStickView.xDelta);
tie.setTranslationY(joyStickView.yDelta);
Log.i( "TAG","Bool:"+ joyStickView.joystickIsPressed);
if (joyStickView.joystickIsPressed) {
Toast.makeText(MainActivity.this, "joysitck is pressed", Toast.LENGTH_SHORT).show();
handlerForMovingTie.postDelayed(this, 10);
mainLayout.invalidate();
} }
}
}; };
handlerForMovingTie.post(movingTie);
Thread threadTie = new Thread(movingTie);
threadTie.start();
Log.i("TAG", "Thread:"+Thread.currentThread().getName());
Log.i("tag", "Hello");
padCenter.setOnTouchListener(joyStickView.touchListener);
Thread threadJS = new Thread(runnable);
threadJS.start();
......
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
...@@ -10,11 +10,7 @@ ...@@ -10,11 +10,7 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:id="@+id/main" android:id="@+id/main"
android:layout_marginBottom="4dp" android:layout_marginBottom="4dp">
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent" >
<ImageView <ImageView
android:id="@+id/fond" android:id="@+id/fond"
...@@ -26,6 +22,7 @@ ...@@ -26,6 +22,7 @@
app:srcCompat="@drawable/etoilefond" /> app:srcCompat="@drawable/etoilefond" />
<FrameLayout <FrameLayout
android:id="@+id/gameSpace"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="199dp" android:layout_height="199dp"
android:layout_alignParentStart="true" android:layout_alignParentStart="true"
...@@ -33,7 +30,8 @@ ...@@ -33,7 +30,8 @@
android:layout_alignParentBottom="true" android:layout_alignParentBottom="true"
android:layout_marginStart="0dp" android:layout_marginStart="0dp"
android:layout_marginLeft="0dp" android:layout_marginLeft="0dp"
android:layout_marginBottom="0dp"> android:layout_marginBottom="0dp" >
<ImageView <ImageView
android:id="@+id/padCenter" android:id="@+id/padCenter"
...@@ -90,4 +88,4 @@ ...@@ -90,4 +88,4 @@
app:srcCompat="@drawable/asteroid1" /> app:srcCompat="@drawable/asteroid1" />
</FrameLayout> </FrameLayout>
</RelativeLayout> </RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout> </RelativeLayout>
\ No newline at end of file \ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="JoystickView">
<attr name="joystickIsPressed" format="boolean"/>
</declare-styleable>
</resources>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment