Combine two Draggable images together and change to another image for every iteration

Hi I want to create this screen where when users drag one image (apparatus) onto another image.

If the combination is true (according to the the procedure), it will change into another image.

  • Example : nailvarnish.png dragged onto hibiscus.png will turn into nailvarnishstroke.png

Else it will return an alert saying “Wrong steps. Try again.”

As of the codes below, the dragging the images part is working. I figured maybe I have to use the combinedRules for every drag and drop combination, but since we are iterating through 7 procedures, I am stuck to figure out how to call it for every iteration.

It would be great if anyone can help me work on this feature! Long story short, this is a scientific version of the alchemy game 🙂

import React, { useState } from 'react';
import { View, Text, StyleSheet, Image, ScrollView, TouchableOpacity } from 'react-native';
import Draggable from 'react-native-draggable';
import Header2 from '../Header2';
import ArrowButtonNext from '../components/ArrowButtonNext';
import ArrowButtonPrevious from '../components/ArrowButtonPrevious';

const ES_LandingPage = ({ navigation }) => {
  const [activeProcedure, setActiveProcedure] = useState(0);
  const [combined, setCombined] = useState({});

  const procedures = [
    {
      id: 1,
      text:
        '1. Apply a thin layer of transparent nail varnish on an area of 5mm x 5mm on the upper surface of the hibiscus leaf and water lily plant.',
      apparatus: ['hibiscus', 'waterlily', 'nail_varnish', 'nail_varnish'],
    },
    {
      id: 2,
      text:
        '2. Allow the varnished leaves to dry.',
      apparatus: ['hibiscus', 'waterlily', 'sun'],
    },
    {
      id: 3,
      text:
        '3. Carefully peel off the layer of nail varnish from the surface of the leaves using a pair of forceps.',
      apparatus: ['hibiscus', 'waterlily', 'forceps'],
    },
    {
      id: 4,
      text:
        '4. Place the peeled-off layer of nail varnish in a drop of water on a microscope slide.',
      apparatus: ['nailvarnishstroke', 'm_slip'],
    },
    {
      id: 5,
      text:
        '5. Cover the slide with a cover slip.',
      apparatus: ['m_slip', 'm_slip'],
    },
    {
      id: 6,
      text:
        '6. Observe the surface of the hibiscus leaf and water lily leaf using low power on a microscope.',
      apparatus: ['microscope', 'hibiscus', 'waterlily'],
    },
    {
      id: 7, 
      text:
        '7. Count and record the number of stomata present.',
      apparatus: ['microscope', 'hibiscus', 'waterlily'],
    },
    // Add more procedures as needed
  ];

  const images = {
    nail_varnish: require('./apparatus/nailvarnish.png'),
    hibiscus: require('./apparatus/hibiscus.png'),
    waterlily: require('./apparatus/waterlily.png'),
    m_slip: require('./apparatus/m_slip.png'),
    forceps: require('./apparatus/forceps.png'),
    nailvarnishstroke: require('./apparatus/nailvarnishstroke.png'),
    sun: require('./apparatus/sun.png'),
    microscope: require('./apparatus/microscope.png'),




    // Add more as needed
  };


  const combinationRules = {
    1: {
      'nailvarnish': {
        target: 'waterlily',
        combinedImage: 'nailvarnishstroke'
      },
      'nailvarnish': {
        target: 'hibiscus',
        combinedImage: 'nailvarnishstroke'
      }
    },
    2: {
      'nailvarnish': {
        target: 'hibiscus',
        combinedImage: 'nailvarnishstroke'
      }
    },
    // ...
  };

  const handleCombination = (apparatus, targetApparatus, procedureId, event) => {
    const rule = combinationRules[procedureId]?.[apparatus];

    if (rule && targetApparatus === rule.target) {
      // Adjust these coordinates to match your drop zone
      if (event.nativeEvent.pageX > 200 && event.nativeEvent.pageX < 900 && 
          event.nativeEvent.pageY > 500 && event.nativeEvent.pageY < 700) {
        setCombined((prev) => ({
          ...prev,
          [procedureId]: rule.combinedImage
        }));
      }
    }
  };

  const handleNextProcedure = () => {
    if (activeProcedure < procedures.length - 1) {
      setActiveProcedure(activeProcedure + 1);
      // setCombined(false); // Add this line
    }
  };
  
  const handlePrevProcedure = () => {
    if (activeProcedure > 0) {
      setActiveProcedure(activeProcedure - 1);
      // setCombined(false); // Add this line
    }
  };

  return (
    <ScrollView contentContainerStyle={styles.scrollViewContent}>
      <>
        <Header2 navigation={navigation} />
        <View style={styles.container}>
          <View style={styles.dropZone} />

          <View style={styles.draggableContainer}>
            {procedures[activeProcedure]?.apparatus?.map((apparatus, index) => (
              <Draggable 
                key={index} 
                style={styles.draggableItem}
                onDragRelease={(event) => {
                  handleCombination(apparatus, 'nailvarnish', activeProcedure, event); // Adjust 'beaker' to your needs
                }}
              >
                <Image 
                  source={combined[activeProcedure] === apparatus ? images[combined[activeProcedure]] : images[apparatus]} 
                  style={styles.image} 
                />
              </Draggable>
            ))}
          </View>

          <Text style={styles.instructions}>{procedures[activeProcedure].text}</Text>

          <View style={{ flexDirection: 'row', justifyContent: 'space-between' }}>
  <ArrowButtonPrevious onPress={handlePrevProcedure} />
  <ArrowButtonNext onPress={handleNextProcedure} />
</View>
        </View>
      </>
    </ScrollView>
  );
};

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
  },
  overlay: {
    position: 'absolute',
    top: 50, // Adjust the position as needed
    left: 100,
    opacity: 0.7, // Set the desired opacity
  },
  dropZone: {
    width: 500,
    height: 500,
    backgroundColor: 'white',
    borderWidth: 2,
    borderColor: 'black',
    marginBottom: 20,
  },
  draggableContainer: {
    flexDirection: 'row',
    justifyContent: 'space-around',
  },
  draggableItem: {
    width: 50,
    height: 50,
    alignItems: 'center',
    justifyContent: 'center',
  },
  image: {
    width: 200,
    height: 200,
    resizeMode: 'contain'
  },
  instructions: {
    fontSize: 16,
    textAlign: 'center',
    padding:30,
  },
  scrollViewContent: {
    flexGrow: 1,
    justifyContent: 'center',
  },
});

export default ES_LandingPage;

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật